ios - 从前台 NSURLSession 移动到后台 NSURLSession - 处理进程中的任务
<p><p>如果应用程序进入后台(例如按下主页按钮),我正在尝试正确处理进程中的 NSURLSessionTasks。我目前正在采取将进程内任务复制到后台队列的方法(参见下面的代码)。但是,我发现后台任务的行为不规律并且并不总是完成。任何人都可以发现我可能做错了什么/建议最好的方法吗?</p>
<pre><code>- (void)appWillResignActive : (NSNotification *)notification {
UIApplication *app = ;
// Register expiring background task
__block UIBackgroundTaskIdentifier bgTaskId =
[app beginBackgroundTaskWithExpirationHandler:^{
bgTaskId = UIBackgroundTaskInvalid;
}];
;
;
}
- (void)appWillBecomeActive : (NSNotification *)notification {
;
}
- (void)switchToBackground
{
NSLog(@"Switch to background line 217 Network Manager");
if () {
[urlSession getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
for (NSURLSessionDownloadTask *downloadTask in downloadTasks) {
[downloadTask cancelByProducingResumeData:^(NSData *resumeData) {
NSURLSessionDownloadTask *downloadTask = ;
;
}];
}
}];
state = kdownloadManagerStateBackground;
}
}
- (void)switchToForeground
{
if () {
[backgroundSession getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
for (NSURLSessionDownloadTask *downloadTask in downloadTasks) {
[downloadTask cancelByProducingResumeData:^(NSData *resumeData) {
NSURLSessionDownloadTask *downloadTask = ;
;
}];
}
}];
state = kdownloadManagerStateForeground;
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><blockquote>
<p>Background sessions let you perform uploads and downloads of content in the background while your app is not running. You can create a background session configuration by calling the backgroundSessionConfiguration: method on the NSURLSessionConfiguration class.</p>
</blockquote>
<pre><code>NSMutableURLRequest *request = ;
NSURLSessionConfiguration *sessionConfig;
float timeout = 5 * 60.0f;
BOOL iOS8OrNew = [[ systemVersion] floatValue] >= 8.0;
if (iOS8OrNew) {
sessionConfig = ;
request.timeoutInterval = timeout;
}
else {
sessionConfig = ;
sessionConfig.timeoutIntervalForRequest = timeout;
}
sessionConfig.HTTPMaximumConnectionsPerHost = 10;
AFURLSessionManager *manager = [ initWithSessionConfiguration:sessionConfig];
NSURLSessionDownloadTask *downloadTask = ;
[manager setDidFinishEventsForBackgroundURLSessionBlock:^(NSURLSession * _Nonnull session) {
AppDelegate *appDelegate = (AppDelegate *)[ delegate];
if (appDelegate.backgroundSessionCompletionHandler) {
void (^completionHandler)() = appDelegate.backgroundSessionCompletionHandler;
appDelegate.backgroundSessionCompletionHandler = nil;
completionHandler();
}
NSLog(@"All tasks are finished");
}];
</code></pre>
<p>更多信息请看我的回答:<a href="https://stackoverflow.com/questions/38195497/how-to-keep-downloading-new-images-in-background-even-if-user-force-quits-the-ap/38195715#38195715" rel="noreferrer noopener nofollow">How to keep downloading new images in background even if user force quits the app in iOS objective C?</a> </p></p>
<p style="font-size: 20px;">关于ios - 从前台 NSURLSession 移动到后台 NSURLSession - 处理进程中的任务,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38306102/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38306102/
</a>
</p>
页:
[1]