【问题标题】:check when two NSOperationQueue have finished to call endBackgroundTask for stopping background task mode检查两个 NSOperationQueue 何时完成调用 endBackgroundTask 以停止后台任务模式
【发布时间】:2012-11-27 00:24:21
【问题描述】:

在我的应用程序中,我有一些 NSOperation 从在线数据库更新一些核心数据元素,有时更新需要一些时间,当 iPhone 屏幕锁定时,应用程序进入后台模式,并且此更新停止,所以我必须重新打开应用程序才能继续更新,所以我刚才在 SO 上问过这个问题,我收到了这个正确答案:

- (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,所以我不能这样称呼:

endBackgroundTask:taskId

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

【问题讨论】:

  • 您可以使用NSOperations 的完成块来找出这个对吗?某种机制将根据此完成块调用检测所有操作是否已完成,一旦总计数为零,您可以执行endBackgroundTask
  • 你能给我写个例子吗?
  • 或者检查这个,operationCountNSOperationQueue 的属性。 developer.apple.com/library/mac/documentation/Cocoa/Reference/…
  • 我知道那个属性...但是我必须在哪里使用它?...请给我一个例子...
  • 我找不到任何 NSOperationQueue 的完成块

标签: iphone ios background-process nsoperation nsoperationqueue


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 2019-11-16
  • 1970-01-01
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-12
相关资源
最近更新 更多