且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用NSOperation和NSOperationQueue iOS的后台任务

更新时间:2023-02-09 18:06:13

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

Why not try doing this? .. I haven't yet tried it out, I'll post an update after attempting this

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

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

also ensure that you have a way to cancel all these long standing operation in the background

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