NSOperation和NSOperationQueue iOS的后台任务

时间:2012-07-07 16:47:06

标签: ios cocoa-touch ios4

我想上传一些即使应用程序进入后台也应该继续的文件。

目前我正在从数据库中检索文件,而不是通过NSOperation将其添加到队列中,然后开始上传过程。

即使应用程序转到后台或前台,也应上传所有文件。 下面是单个任务的代码,任何人都可以给我一个提示,告诉我们如何使其上传许多文件。

UIApplication* application = [UIApplication sharedApplication];
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
  // Clean up any unfinished task business by marking where you
  // stopped or ending the task outright.

  [application endBackgroundTask: bgTask];
  bgTask = UIBackgroundTaskInvalid;
}];

2 个答案:

答案 0 :(得分:0)

如果您的应用遵循并满足必要条件,Apple允许后台执行。             plese refer here

答案 1 :(得分:0)

为什么不尝试这样做? ..我还没试过,我会在尝试这个后发布更新

UIApplication* application = [UIApplication sharedApplication];
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
  // Clean up any unfinished task business by marking where you
  // stopped or ending the task outright.

  // Bring up your NSOperation queue instance here and block this thread until it is complete
  [queue waitUntilAllOperationsAreFinished];

  [application endBackgroundTask: bgTask];
  bgTask = UIBackgroundTaskInvalid;
}]; 

还确保您有办法在后台取消所有这些长期操作

 bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        // Clean up any unfinished task business by marking where you.
        // stopped or ending the task outright.
        [queue cancelAllOperations];

        [application endBackgroundTask:endSessionTask];
        bgTask = UIBackgroundTaskInvalid;
    }];