蜂窝网络上的奇怪 NSURLSessionDownloadTask 行为(不是 wifi) [英] Weird NSURLSessionDownloadTask behavior over cellular (not wifi)

查看:41
本文介绍了蜂窝网络上的奇怪 NSURLSessionDownloadTask 行为(不是 wifi)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已启用具有远程通知任务的后台模式,以在应用收到推送通知时在后台下载一个小文件 (100kb).我已经使用

I've enabled Background Modes with remote-notification tasks to download a small file (100kb) in background when the app receives a push notification. I've configured the download Session using

NSURLSessionConfiguration *backgroundConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:sessionIdentifier];
[backgroundConfiguration setAllowsCellularAccess:YES];


self.backgroundSession = [NSURLSession sessionWithConfiguration:backgroundConfiguration
                                                       delegate:self
                                                  delegateQueue:[NSOperationQueue mainQueue]];

并使用激活它

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[hostComponents URL]];

[request setAllowsCellularAccess:YES];


NSMutableData *bodyMutableData = [NSMutableData data];
[bodyMutableData appendData:[params dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[bodyMutableData copy]];


_downloadTask =  [self.backgroundSession downloadTaskWithRequest:request];

[self.downloadTask resume];

现在,只有当我通过 Wifi 或蜂窝网络连接但 iPhone 通过电缆连接到 xCode 时,一切才能正常工作,如果我断开 iPhone 并通过蜂窝网络收到推送通知,代码会在 [self.downloadTask resume]; 没有调用 URL 的行.

Now everything works correctly only if I'm connected over Wifi or over Cellular but with the iPhone connected with the cable to xCode, if I disconnect the iPhone and receive a push notification over cellular the code stop at [self.downloadTask resume]; line without call the URL.

处理这些操作的类是NSURLSessionDataDelegate、NSURLSessionDownloadDelegate、NSURLSessionTaskDelegate等实现:

The class that handles these operation is a NSURLSessionDataDelegate, NSURLSessionDownloadDelegate, NSURLSessionTaskDelegate and so implements:

    - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location

我尝试在 [self.downloadTask resume] 之后插入带有 UILocalNotification 的调试行(显示为现在"),但在 5 分钟后调用表示 self.downloadTask.state 已暂停"

I've try to insert a debug line with a UILocalNotification (presented 'now') after the [self.downloadTask resume] but is called after 5 minutes and says that the self.downloadTask.state is 'suspended'

这种奇怪的行为是什么原因造成的?

What is due this weird behavior ?

推荐答案

NSURLSessionConfiguration 类参考文档在这里:

The documentation for NSURLSessionConfiguration Class Reference here:

https://developer.apple.com/Library/ios/documentation/Foundation/Reference/NSURLSessionConfiguration_class/Reference/Reference.html#//apple_ref/occ/instp/NSURLSessionConfiguration/discretionary

说:对于可自由支配的财产:

Says: for the discretionary property:

讨论

设置此标志时,插入时更有可能发生传输接通电源并连接到 Wi-Fi.该值默认为 false.

When this flag is set, transfers are more likely to occur when plugged into power and on Wi-Fi. This value is false by default.

只有当会话的配置对象是最初通过调用backgroundSessionConfiguration构造:方法,并且仅适用于在应用程序处于前台时启动的任务.如果在应用程序处于后台时启动任务,则该任务是视为酌情决定是真实的,而不管实际情况如何此属性的值. 对于基于其他属性创建的会话配置,此属性将被忽略.

This property is used only if a session’s configuration object was originally constructed by calling the backgroundSessionConfiguration: method, and only for tasks started while the app is in the foreground. If a task is started while the app is in the background, that task is treated as though discretionary were true, regardless of the actual value of this property. For sessions created based on other configurations, this property is ignored.

这似乎意味着,如果在后台开始下载,操作系统始终可以自行决定是否以及何时继续下载.在完成这些任务之前,操作系统似乎总是在等待 wifi 连接.

This seems to imply that if a download is started in the background the OS always has discretion as to whether and when to proceed with the download. It seems that the OS is always waiting for a wifi connection before completing these tasks.

我的经验支持这个猜想.我发现当设备使用蜂窝网络时,我可以发送多个下载通知.他们仍然卡住.当我将设备切换到 wifi 时,它们都通过了.

My experience supports this conjecture. I find that I can send several notifications for downloads while device is on cellular. They remain stuck. When I switch the device to wifi they all go through.

这篇关于蜂窝网络上的奇怪 NSURLSessionDownloadTask 行为(不是 wifi)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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