我在UIBackgroundTaskIdentifier下运行一个特定的任务,因为我想在后台运行它。我的代码看起来像这样。
-(void) function
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
UIBackgroundTaskIdentifier BGIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{}];
// some processing
dispatch_async(dispatch_get_main_queue(), ^{
// some UI stuff
});
// some processing again
dispatch_async(dispatch_get_main_queue(), ^{
// some UI stuff again
});
[[UIApplication sharedApplication] endBackgroundTask:BGIdentifier];
});
}所以我有两个问题。
发布于 2014-02-26 06:42:21
在回答您的问题时,那些发送到主队列的块按照您的预期操作,一切都会正常工作,当应用程序重新进入前台时,您将看到UI被正确更新。两个注意事项:
endBackgroundTask。您可以通过以下两种方法来实现这一目标:- Make that final UI dispatch synchronous; or
- Include the `endBackgroundTask` as the last item in the last block you dispatch to the main queue.但是这个endBackgroundTask的时间很重要,您需要确保在将后台任务标记为完成之前更新UI,并指出它可能被挂起。
endBackgroundTask,因为如果后台任务在分配的时间内未完成,则应用程序将立即终止。有关示例,请参阅App的在后台执行有限长度任务。和iOS应用程序编程指南中的多任务处理一章。https://stackoverflow.com/questions/22033080
复制相似问题