I’m writing an iPhone app that interacts with a RESTful web service, and I need to request an authentication token for use with all later requests. I want to avoid multithreading if possible and instead use the asynchronous NSURLConnection methods to ensure that the main thread doesn’t block (and thereby ensure that the user interface remains responsive).
The problem I’m running into is that I can’t figure out how to make the later requests wait for the token to be fetched without making the token fetch a synchronous request and blocking the main thread.
It’s occurred to me that I could toss in a loop with a semaphore value that’s set when the token fetch completes, but that seems clumsy and the wrong way to do it. Is there an accepted way of doing this correctly?
If you’re building for iOS 5.0 or later, you can use:
Make your subsequent call in the
completionHandler:block.