ios - NSOperation和NSOperationQueue iOS的后台任务

标签 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;
}];

最佳答案

为什么不尝试这样做呢? ..我尚未尝试过,尝试后将发布更新

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;
    }];

关于ios - NSOperation和NSOperationQueue iOS的后台任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11376873/

相关文章:

iphone - 使用 SDK 4.0 模拟 iPhone 3.0

ios - 不同的iphone是否具有相同的加速度计轴?

iphone - 跳过成员变量的“self”关键字会使程序崩溃

ios - 如何过滤无效位置更新 iOS

ios - 使用 PresentModalViewController 更改 View 时出现 EXC_BAD_ACCESS 错误

objective-c - UIScrollView 内的 UIView 未获取触摸事件

iphone - 如何在iPad App中显示这个?

ios - iPhone : Is it possible to close the app rather than run in background when user clicks Home Button?

ios - 两个不直接连接的 ViewController 之间的通信

ios - NSCache、镜像发布、异步队列