蜂窝(未WIFI)怪异行为NSURLSessionDownloadTask [英] Weird NSURLSessionDownloadTask behavior over cellular (not wifi)

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

问题描述

我已经启用远程通知任务背景模式时,应用程序收到一个推送通知,下载一个小文件(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];

现在一切正常,只有当我连接了Wi-Fi或蜂窝以上,但与电缆为x code连接的iPhone,如果我断开iPhone和接收蜂窝的code推送通知停在[self.downloadTask简历]行不调用的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简历]之后,但5分钟后称为UILocalNotification(psented'现在'$ P $)调试行并表示,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:

<一个href=\"https://developer.apple.com/Library/ios/documentation/Foundation/Reference/NSURLSessionConfiguration_class/Reference/Reference.html#//apple_ref/occ/instp/NSURLSessionConfiguration/discretionary\">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:

讨论

在此标志设置,传输更容易插入时发生
  到电源和无线网络连接。此值默认为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.

这似乎在暗示,如果下载是在后台启动的操作系统总是有酌情决定是否和何时与执行下载。看来,OS总是等待完成这些任务之前,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.

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

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