【问题标题】:Background Task with NSOperation and NSOperationQueue iOS带有 NSOperation 和 NSOperationQueue iOS 的后台任务
【发布时间】:2012-07-07 18:41:00
【问题描述】:

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

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

【问题讨论】:

    标签: ios cocoa-touch ios4


    【解决方案1】:

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

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

    【讨论】:

    • 我不认为这个答案是正确的。 waitUntilAllOperationsAreFinished 调用应该在beginBackgroundTaskWithExpirationHandler 之后进行,而不是在过期块内。看到这个答案:stackoverflow.com/questions/10319643/…
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多