2

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...)

Community
  • 1
  • 1
oky_sabeni
  • 7,672
  • 15
  • 65
  • 89
  • 1
    Of what type is `self.instanceVariable`? String? – luk2302 Jan 21 '16 at 20:15
  • Where are you checking the value of `self.instanceVariable`? I would suggest logging `self.instanceVariable` just after it is and check for yourself. Its possible you are checking the value of `self.instanceVariable` before its even set. – p0lAris Jan 21 '16 at 20:23
  • I doubt you code. First: defining let result is immutable, the compiler would create an error on assignment. Second: how do you check that "nothing happens"? Of course it would be assigned. But you could have declared your instanceVariable as "weak" then it would be deallocated right after the assignment. Post the full code you are really using. – Darko Jan 21 '16 at 21:14
  • @luk2302. AnyObject really – oky_sabeni Jan 21 '16 at 21:40

1 Answers1

1

I think you have misunderstood what "only available inside the scope of a response handler" means in this context. There is no mechanism preventing you from passing or copying a value out of the response block.

Alamofire is trying to explain that it provides no synchronous interface to this request's response value and therefore you must use the async handler to trigger your use of the response. You're free to copy the response value out of the response handler but the variable you're setting will not be set until the response handler executes some time in the future. The Doesn't do anything comment in the question above is probably an example of this. It's not that the variable is not set but rather than some other branch of code is checking the value of that variable before the completion handler has executed and finding it to still be nil.

Jonah
  • 17,918
  • 1
  • 43
  • 70
  • @okysabeni if this is new to you then a related concern you will eventually discover is that this closure (the response handler) captures any object you reference inside it. In the question here that means it creates a strong reference to `self`. That can have surprising side effects like keeping a view controller around until a network request completes even though the controller was popped off a navigation controller stack long ago. See http://stackoverflow.com/questions/24320347/shall-we-always-use-unowned-self-inside-closure-in-swift – Jonah Jan 22 '16 at 00:08
  • @okysabeni `[unowned self]` does not retain `self` but also cannot tolerate it becoming `nil` so a block using `[unowned self]` after the `self` has been deallocated will crash. – Jonah Jan 22 '16 at 00:31
  • Yea. I think I got it. The problem was completely not related to this. Apparently, one of our data structures was created incorrectly and was immutable so it never sets it. Thanks though – oky_sabeni Jan 22 '16 at 00:40