From the Alamofire page,
Rather than blocking execution to wait for a response from the server, a callback is specified to handle the response once it's received. The result of a request is only available inside the scope of a response handler. Any execution contingent on the response or data received from the server must be done within a handler.
I understand that but what actually happens when you try to set your variable?
For example:
class Person: NSObject {}
var person = Person()
Alamofire
.request(APIRouter.GetUpComingRides(parameters))
.responseJSON { response in
// Doesn't do anything
self.instanceVariable = response.result.value
}
}
Using unsafeAddressOf
, I know that response.result.value
actually have an address in memory. So, what happens when you set your variable to that?
If I use a value type instead of a reference type, it will work.
struct Person {}
var person: Person
Alamofire
.request(APIRouter.GetUpComingRides(parameters))
.responseJSON { response in
// Actually sets the result to the value
self.result = response.result.value
}
}
Reading the NSURLSession
document, dataTaskWithRequest
which is what Alamofire calls, does the operation on the delegate
queue. There must be something that disables setting the pointer of my instance variable object to an object in a delegate queue.
EDIT: For more clarification: See How to return value from Alamofire
You actually don't have to use the completion handler pattern if you use value types (structs, strings, int, etc...)