当应用程序在后台运行时,不会调用AFHTTPSessionManager块 [英] AFHTTPSessionManager block is not called when app is on background

查看:170
本文介绍了当应用程序在后台运行时,不会调用AFHTTPSessionManager块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序无声地推送,然后使用AFNetworking在后台执行一些http请求,但未输入完整的代码块,代码为:

My app get a silent push, then do some http request in background using AFNetworking,but it didn't enter the complete block, code is:

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
[manager GET:urlString
  parameters:nil
     success:^(NSURLSessionDataTask *task, id responseObject) {
         NSLog(@"response objece:%@", responseObject);
     }
     failure:^(NSURLSessionDataTask *task, NSError *error) {
         NSLog(@"error:%@", error);
     }];

然后我发现也许我可以使用NSURLSessionConfiguration来配置会话:

then I found maybe I could use NSURLSessionConfiguration to config the session:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.company.backgroundDownloadSession"];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
[manager GET:urlString
  parameters:nil
     success:^(NSURLSessionDataTask *task, id responseObject) {
         NSLog(@"response objece:%@", responseObject);
     }
     failure:^(NSURLSessionDataTask *task, NSError *error) {
         NSLog(@"error:%@", error);
     }];

但AFNetworking崩溃表示:由于未捕获的异常'NSGenericException',正在终止应用程序,原因:'数据任务在后台会话中不受支持。'
我应该怎么办?感谢您的帮助!

but AFNetworking crash says:'Terminating app due to uncaught exception 'NSGenericException', reason: 'Data tasks are not supported in background sessions.' What should I do? And I appreciate your help!

推荐答案

一些想法:


  1. 正确的背景 NSURLSessionConfiguration 需要 NSURLSessionDownloadTask NSURLSessionUploadTask 。但是, GET 方法将创建 NSURLSessionDataTask

  1. Proper background NSURLSessionConfiguration requires NSURLSessionDownloadTask or NSURLSessionUploadTask. The GET method, though, creates NSURLSessionDataTask.

要使用下载或上传任务,您必须单独构建请求,然后利用 AFURLSessionManager 发布下载或上传。话虽如此,但是,如果您尝试创建HTTP GET / POST样式的请求,则可以使用各种请求序列化器来创建请求。只需使用 AFHTTPRequestSerializer 方法 requestWithMethod

To use download or upload tasks, you'll have to build your request separately, and then leverage AFURLSessionManager to issue download or upload. Having said that, you can, though, create requests using the various request serializers, if you're trying to create HTTP GET/POST style requests. Just use the AFHTTPRequestSerializer method requestWithMethod.

将AFNetworking与背景 NSURLSessionConfiguration 结合使用的基本介绍,请参见 https:// stackoverflow .com / a / 21359684/1271826 。您必须将其与上述的 requestWithMethod 结合使用。

For a basic introduction to using AFNetworking in conjunction with background NSURLSessionConfiguration, see https://stackoverflow.com/a/21359684/1271826. You'll have to marry that with the requestWithMethod, discussed above.

请注意,使用特定于任务的完成块(因为即使应用程序终止并且这些块早已消失,任务仍会继续执行)。如AFNetworking文档的后台下载任务所述:

Note, be wary about using the task-specific completion blocks (because the tasks continue even if the app is terminated and these blocks are long gone). As the AFNetworking documentation for the background download tasks says:


警告:如果使用后台 NSURLSessionConfiguration 在iOS上,当应用终止时,这些块将丢失。后台会话可能更喜欢使用 setDownloadTaskDidFinishDownloadingBlock:指定用于保存下载文件的URL,而不是此方法的目标块。

Warning: If using a background NSURLSessionConfiguration on iOS, these blocks will be lost when the app is terminated. Background sessions may prefer to use setDownloadTaskDidFinishDownloadingBlock: to specify the URL for saving the downloaded file, rather than the destination block of this method.


  • 如果您提出的要求不高,如果用户恰巧在离开应用程序的同时离开了应用程序,可能只要求OS稍作一些操作就比较容易。请求仍在进行中。请参阅执行有限长度任务部分。 rel = nofollow noreferrer> iOS应用程序编程指南:后台执行。

  • If you're making a modest request, it may be easier to just ask the OS for a little time to do so if the user happens to leave your app while the request is still in progress. See Executing Finite-Length Tasks section of App Programming Guide for iOS: Background Execution.

    底线在发出请求之前,请执行以下操作:

    Bottom line, before issuing the request, do something like:

    UIApplication *application = [UIApplication sharedApplication];
    
    bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
        // Clean up any unfinished task business by marking where you
        // stopped or ending the task outright.
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];
    

    然后在请求的完成块中,您可以终止后台任务:

    And then in the completion block of the request, you can terminate the background task:

    if (bgTask != UIBackgroundTaskInvalid) {
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }
    

    这仅适用于有限长度的任务,但它比尝试执行起来容易得多做后台 NSURLSessionConfiguration

    This is only good for finite length tasks, but it probably much easier than trying to do background NSURLSessionConfiguration.

    这篇关于当应用程序在后台运行时,不会调用AFHTTPSessionManager块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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