我在测试中发现一个错误,偶尔会收到以下 iOS 错误:
标识符为 {GUID} 的后台 URLSession 已存在!
即使我在每次测试后的清理调用中对 NSURLSession 调用 invalidateAndCancel 。我正在寻找一种方法来等待,直到我确定 NSURLSession 对象已失效,然后再进行下一个测试。
当您创建您的NSURLSession
对象使用sessionWithConfiguration:(NSURLSessionConfiguration *)configuration delegate:(id <NSURLSessionDelegate>)delegate delegateQueue:(NSOperationQueue *)queue;
方法并将您自己指定为委托人。
然后您将收到回调:- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error;
当会话失效时。
你不能相信这一点invalidateAndCancel
将立即停止所有任务,因为由任务来处理取消调用。
从标题NSURLSession
:
/* -invalidateAndCancel acts as -finishTasksAndInvalidate, but issues
* -cancel to all outstanding tasks for this session. Note task
* cancellation is subject to the state of the task, and some tasks may
* have already have completed at the time they are sent -cancel.
*/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)