2

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

目前我正在从数据库中检索文件,然后通过 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;
}];
4

2 回答 2

0

如果您的应用程序遵循并满足必要条件,Apple 允许后台执行。 请参考这里

于 2012-07-10T09:17:16.390 回答
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;
    }];
于 2012-11-05T23:49:10.957 回答