我正在尝试运行后台 NSURLSessionDownloadTask。但是,我注意到在任务实际开始之前大约需要 30 秒。当我使用 defaultSessionConfiguration 时,任务几乎立即开始。有没有办法在使用 backgroundSessionConfiguration 时立即触发 NSURLSessionDownloadTask?
下面是我使用 backgroundSessionConfiguration 时得到的结果:
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:[NSString stringWithFormat:bgIdentifier, self.urlModel.code]];
这是日志输出。请注意,在调用“resume”方法之前会记录“Start uploading...”,并且在 NSURLSessionTaskDelegate 的 didSendData 方法中记录“Upload progress”。
2014-02-18 20:13:55.403 myapp[396:60b] [DoneViewController] Start uploading...
2014-02-18 20:14:17.699 myapp[396:60b] [DoneViewController] Upload progress - 1%
2014-02-18 20:14:17.704 myapp[396:60b] [DoneViewController] Upload progress - 2%
2014-02-18 20:14:17.705 myapp[396:60b] [DoneViewController] Upload progress - 3%
2014-02-18 20:14:17.706 myapp[396:60b] [DoneViewController] Upload progress - 4%
仅供参考,相同的东西,但使用默认会话配置。
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
您可以看到控制台结果没有延迟是这样的:
2014-02-18 19:57:00.552 myapp[376:60b] [DoneViewController] Start uploading...
2014-02-18 19:57:00.707 myapp[376:60b] [DoneViewController] Upload progress - 2%
2014-02-18 19:57:00.710 myapp[376:60b] [DoneViewController] Upload progress - 5%
2014-02-18 19:57:00.711 myapp[376:60b] [DoneViewController] Upload progress - 8%
2014-02-18 19:57:00.711 myapp[376:60b] [DoneViewController] Upload progress - 11%
编辑:使用 iOS 7.1,有时没有延迟。但是,仍然有大约 20 秒的延迟。