在iOS上使用后台任务进行UI更新 [英] UI-updates with Background tasks on iOS
问题描述
我有一个同步过程,有时(甚至很少)需要20秒。我曾经在后台调用选择器。有一个进度指示器,它会在通知同步完成后停止播放动画,并且其下面的代码会立即更改:
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?
编辑:
通知侦听器是:
The notification listener is:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(refreshBttn)
name:@"finishedUpdate"
object:nil];
代码为:
- (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];
}
推荐答案
您需要拆分后台线程和前台线程之间的逻辑。您发送的通知应从主线程发送,以便您的UI对象可以安全地进行更新。因此,将 updateFunction
分为两部分。也许是这样的:
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 updateFunction
into two pieces. Perhaps something like this:
NSLog(@"\n\nRunning in the background!\n\n");
[self updateFunctionBackground];
dispatch_async(dispatch_get_main_queue(), ^{
// send your notification here instead of in updateFunction
[[NSNotificationCenter defaultCenter] post...];
});
这篇关于在iOS上使用后台任务进行UI更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!