ios - Making HTTP request with AlamoFire causes EXC_BAD_ACCESS -
i'm working on simple project learn swift , i'm running problem think has deeper implications / learning opportunities (trying on bright side).
the high-level problem when post json-encoded parameters using alamofire, exc_bad_access error appears in separate line of code set 1 of parameters (specifically corelocation manager's "didupdatelocations") ... here's code:
in viewcontroller, create mutable dictionary:
var parameters = [string:anyobject]()
and, didupdatelocations
event, assign updated latitude / longitude values respective keys in mutable dictionary.
class viewcontroller: uiviewcontroller, cllocationmanagerdelegate { let appdelegate = uiapplication.sharedapplication().delegate appdelegate ... func locationmanager(locmanager: cllocationmanager!, didupdatelocations locations: [anyobject]!) { appdelegate.parameters["longitude"] = locmanager.location.coordinate.longitude appdelegate.parameters["latitude"] = locmanager.location.coordinate.latitude }
finally, have periodic function (using nstimer.scheduledtimerwithtimeinterval
) posts server.
func updatepost() { println("posting update") alamofire.request(.post, "http://server.co/endpoint", parameters: appdelegate.parameters, encoding: .json) }
if comment out alamofire post, fine. with post, exc_bad_access error @ first line of didupdatelocations (where longitude
key set)
i suspect it's how parameters converted encoding routine of alamofire, have no idea why appear in didupdatelocations function , not in alamofire call ...
can provide insights? thank you
what's happening have multiple threads trying access dictionary
@ same time. when modify parameters
in didupdatelocations
, moves in memory while it's being read inside alamofire, causing exc_bad_access exception.
to solve stop updating parameters
dictionary within didupdatelocations
- add latestlocation
property view controller , update instead. then, inside updatepost
, create dictionary of parameters , pass alamofire.request
.
Comments
Post a Comment