问题描述
我在测试中看到一个错误,偶尔会出现以下iOS错误:
I am seeing an error in my tests where occasionally I get the following iOS error:
标识为{GUID}的后台URLSession已经存在!
A background URLSession with identifier {GUID} already exists!
即使我在每次测试后在清理调用中调用NSURLSession上的invalidateAndCancel。我正在寻找一种方法,等到我确定NSURLSession对象已经失效,然后再进行下一次测试。
Even though I call invalidateAndCancel on the NSURLSession in the cleanup call after every test. I am looking for a way to wait until I am sure the NSURLSession object has been invalidated, before proceeding to the next test.
推荐答案
当您创建 NSURLSession
对象时,使用 sessionWithConfiguration:(NSURLSessionConfiguration *)配置委托:(id< NSURLSessionDelegate>)委托delegateQueue:(NSOperationQueue * )queue;
方法并指定你自己作为委托。
When you create your NSURLSession
object use the sessionWithConfiguration:(NSURLSessionConfiguration *)configuration delegate:(id <NSURLSessionDelegate>)delegate delegateQueue:(NSOperationQueue *)queue;
method and specify yourself as the delegate.
然后你将得到一个回调: - (void )URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error;
会话失效时。
You will then get a callback in: - (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error;
when the session is invalidated.
你不能相信 invalidateAndCancel
将立即停止所有任务,因为它取决于处理取消呼叫的任务。
You cannot trust that invalidateAndCancel
will instantly stop all tasks since it is up to the tasks to handle the cancel-call.
从标题中 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.
*/
这篇关于你怎么知道NSURLSession对象何时被iOS无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!