NSURLSessionConfiguration的discretionary属性

2019-07-14 01:02发布

最近使用NSURLSession做下载时,配置了NSURLSessionConfiguration为backgroundSessionConfiguration,
在下载时发现拔下iPhone拔下电源后,点击下载无任何进度,连接电源后即可正常下载,最终发现是NSURLSessionConfiguration的discretionary设置为了YES导致;
此属性在官方API文档的定义为: /* allows background tasks to be scheduled at the discretion of the system for optimal performance. */ @property (getter=isDiscretionary) BOOL discretionary NS_AVAILABLE(10_10, 7_0); 此属性设置为YES时,系统根据当前性能自动处理后台任务的优先级,以获得最佳性能 (仅background session有效),
但是我用的测试机为iPhone7,且电量是满满的。
经查阅:allowsCellularAccess 和 discretionary 被用于节省通过蜂窝连接的带宽。建议在使用后台传输的时候,使用discretionary属性,而不是allowsCellularAccess属性,因为它会把WiFi和电源可用性考虑在内。