Reputation: 13106
I have a need to update some data from a webservice via a background thread in an app I'm working on.
Normally I'd just do this via an NSOperationQueue
and a synchronous web request in the main() function of the NSOperation
.
However, for this specific data fetch, the app requires the use of OAuth
and the OAuthConsumer
library I am using performs the request to the webservice via an asynchronous request with delegate method callbacks for success/failure.
My issue is that those delegate callbacks done seem to get processes, because (as is my understanding) my operation process is destroyed/cleaned up when the end of the main()
function is reached and the operation is pop off the operation queue's stack.
Is this accurate?
If so, is there a solution to doing this via an NSOperationQueue and if not, what is the current best practice for grabbing data via a background thread in an asynchronous manner that can handle delegate callbacks?
Upvotes: 3
Views: 1624
Reputation: 57179
You can continue the run loop until your operations are complete. What you will do is start you asynchronous operations and the keep running the run loop until they are complete. The complete flag will be an instance variable that you will set when all of your async (OAuth) items are done.
complete = NO;
//Start async operations
while (!complete) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
...
-(void)completeCallback
{
complete = YES;
}
Upvotes: 1