检查两个NSOperationQueue是否已完成以调用endBackgroundTask来停止后台任务模式 [英] check when two NSOperationQueue have finished to call endBackgroundTask for stopping background task mode

查看:68
本文介绍了检查两个NSOperationQueue是否已完成以调用endBackgroundTask来停止后台任务模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一些NSOperation可以从在线数据库中更新某些核心数据元素,有时更新需要几分钟,并且当iPhone的屏幕锁定时,应用程序进入后台模式,并且此更新停止,所以我必须重新打开应用程序才能继续更新,所以我在几分钟前就这样问过了,我已经收到了以下正确答案:

in my app i have some NSOperation that update some core data element from a online database, sometime the update require some minute, and when the screen of iPhone lock, the app enter in the background mode, and this update is stopped, so i have to reopen the app to continue the update, so i have asked this some minute ago on SO, i have received this correct answer:

- (void)someMethodToKeepRunningInBackground {
UIBackgroundTaskIdentifier taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
    // Uh-oh - we took too long. Stop task.
}];

// Perform task here
CheckForUpdate *checkUpdate = [[CheckForUpdate alloc] init];
[sectionCheckUpdateQueue addOperation:checkUpdate];       

if (taskId != UIBackgroundTaskInvalid) {
    [[UIApplication sharedApplication] endBackgroundTask:taskId];
}
}

问题是我运行了更多的NSOperationQueue来执行更新,我在上面写了sectionCheckUpdateQueue,如果发现更新计划在其他NSOperationQueue上有更多的NSOperation,所以我不能这样称呼:

the problem is that i have the more NSOperationQueue that run to perform the update, the sectionCheckUpdateQueue that i write above, that if found update schedule more NSOperation on other NSOperationQueue, so i can't call this:

endBackgroundTask:taskId

因为我不知道这个NSOperationQueue何时完成,所以我的问题是,那么我如何才能检测到这个NSOperationQueue何时完成,以便调用endBacgkround:taskId?

because i don't know when this NSOperationQueue are finished, so my question is, so how i can detect when this NSOperationQueue are finished so to call the endBacgkround:taskId?

推荐答案

您可以在 NSOperationQueue 上调用 waitUntilAllOperationsAreFinished 来知道何时完成所有操作.

You could call waitUntilAllOperationsAreFinished on the NSOperationQueue to know when it is done with all operations.

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html#//apple_ref/occ/instm/NSOperationQueue/waitUntilAllOperationsAreFinished

这篇关于检查两个NSOperationQueue是否已完成以调用endBackgroundTask来停止后台任务模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆