I have a synchronisation process which sometimes (even rarely) takes 20 seconds. I used to call the selector in the background. There is a progress indicator which stops animating on a notification that the sync is finished, and with the code below it changes straight away:
[self performSelectorInBackground:@selector(updateFunction) withObject:nil];
But in order to allow the progress to continue I decided to use the background task:
UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance
__block UIBackgroundTaskIdentifier background_task; //Create a task object
background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
[application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
//System will be shutting down the app at any point in time now
}];
//Background tasks require you to use asyncrous tasks
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Perform your tasks that your application requires
NSLog(@"\n\nRunning in the background!\n\n");
[self updateFunction];
[application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
NSLog(@"Finished in the background!");
});
But now, when the task is finished the notifications is sent (and executed) but the cell in the table doesn’t update until (sometimes) a significant amount of time. So it seems to me that the ‘needs display’ is not triggered when coming from a background task or so, and is only updated at a regular refresh interval or so.
Is there a way to have the table cell update using background tasks, or am I missing something?
Edit:
The notification listener is:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(refreshBttn)
name:@"finishedUpdate"
object:nil];
The code is:
- (void) refreshBttn {
NSLog(@"Refresh Buttun action");
iDomsAppDelegate *delegate = (iDomsAppDelegate *)[[UIApplication sharedApplication] delegate];
if([delegate updating]){
[_updateBttn setHidden:TRUE];
[_activityIndicator startAnimating];
} else {
[_updateBttn setHidden:FALSE];
[_activityIndicator stopAnimating];
}
[self setNeedsDisplay];
[self setNeedsLayout];
}
You need to split up your logic between background and foreground threads. The notification you’re sending should be sent from the main thread, so that your UI objects listening to it can update themselves safely. So, break up
updateFunctioninto two pieces. Perhaps something like this: