2012-07-07 82 views
1

我想上传一些应该继续下去的文件,即使应用程序进入后台。NSOperation和NSOperationQueue的后台任务iOS

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

回答

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

我不认为这个答案是正确的。 'waitUntilAllOperationsAreFinished'调用应该在'beginBackgroundTaskWithExpirationHandler'之后进行,而不是在expiration块内。看到这个答案:http://stackoverflow.com/questions/10319643/objective-c-proper-use-of-beginbackgroundtaskwithexpirationhandler – Dima 2014-12-08 22:32:01

相关问题