你怎么知道NSURLSession对象何时被iOS无效?

Spi*_*lly 17 ios nsurlsession

我在测试中看到一个错误,偶尔会出现以下iOS错误:

已存在标识为{GUID}的后台URLSession!

即使我在每次测试后在清理调用中调用NSURLSession上的invalidateAndCancel.我正在寻找一种方法,等待我确定NSURLSession对象已经失效,然后再进行下一个测试.

ABe*_*its 13

创建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. 
*/
Run Code Online (Sandbox Code Playgroud)