我有一个同步过程,有时(甚至很少)需要20秒。我曾经在后台调用选择器。有其停止对同步完成的通知动画进度指示器,并与它下面的代码马上改变:iOS上的后台任务的UI更新
[self performSelectorInBackground:@selector(updateFunction) withObject:nil];
但为了让进度继续我决定使用后台任务:
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!");
});
但是现在,当任务完成后通知被发送(和执行),但在表中的单元格不更新,直到(有时)的时间显著量。所以在我看来,'需求显示'在后台任务等等时不会被触发,而只是在常规的刷新间隔内进行更新。
有没有办法让表格单元更新使用后台任务,或者我错过了什么?
编辑: 通知监听器是:
[[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];
}
这似乎没有什么区别。在这两种情况下,我都可以看到更新单元格时创建的日志条目,但行为没有变化。 (但是说了这样的话,确实是更好,因为它现在在主线程中执行) – 2012-08-06 17:36:11
@ LuukD.Jansen也许问题出在你的UI更新代码中?你在那里做什么?你可以发布该通知的接收者的代码吗? – 2012-08-06 18:28:59
这似乎并不是问题,因为它只是在后台执行更新功能时,在第一个实例中完美工作。我将把代码,但它很简单。 – 2012-08-07 07:04:29