使用NSOperation和NSOperationQueue iOS的后台任务 [英] Background Task with NSOperation and NSOperationQueue iOS

查看:56
本文介绍了使用NSOperation和NSOperationQueue iOS的后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to upload some files which should continue even if the application goes into background.

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

Currently I am retrieving the files from the DB and than adding it to the Queue via NSOperation which is then starts the upload procedure.

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

All the files should be uploaded even if the app goes to background or foreground. Below is the code for single task can anyone give me a hint how we can make it to work for uploading many files.

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


推荐答案

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

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

这篇关于使用NSOperation和NSOperationQueue iOS的后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆