Reputation: 34798
How can I get an indication to main UI thread in an iPhone app IOS program, when background task has completed?
Background
Upvotes: 2
Views: 2057
Reputation: 37053
Once you're finished loading in the background, call the following from your background thread:
[self performSelectorOnMainThread:@selector(backgroundLoadingDidFinish:) withObject:nil waitUntilDone:NO];
And then implement -(void)backgroundLoadingDidFinish:(id)sender
in your RootViewController. If you need to, you can pass data back in the above method (the withObject:
part).
Upvotes: 1
Reputation: 5200
You can call back into the main thread from your background selector using -performSelectorOnMainThread:withObject:waithUntilDone:
like so:
- (void)loadModel
{
// Load the model in the background
Model *aModel = /* load from some source */;
[self setModel:aModel];
[self performSelectorOnMainThread:@selector(finishedLoadingModel) withObject:nil waitUntilDone:YES];
}
- (void)finishedLoadingModel
{
// Notify your view controller that the model has been loaded
[[self controller] modelLoaded:[self model]];
}
Update: an even safer way to do this would be to check in -finishedLoadingModel
to make sure you're running on the main thread:
- (void)finishedLoadingModel
{
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:_cmd withObject:nil waitUntilDone:YES];
}
// Notify your view controller that the model has been loaded
[[self controller] modelLoaded:[self model]];
}
Upvotes: 3