2014-01-28 45 views
13

我在我的測試中看到一個錯誤,偶爾出現以下iOS錯誤:你怎麼知道iOS的NSURLSession對象已經失效?

標識符爲{GUID}的背景URLSession已存在!

即使我在每次測試後在清理調用中的NSURLSession上調用invalidateAndCancel。我正在尋找一種方法來等待,直到我確定NSURLSession對象已經失效,然後繼續進行下一個測試。

回答

11

當您創建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. 
*/ 
3

蘋果

"IMPORTANT

The session object keeps a strong reference to the delegate until your app exits or explicitly invalidates the session. If you do not invalidate the session, your app leaks memory until it exits."

也許代表是無效的會話零(如果你總是使用委託)。雖然同意爲什麼沒有isValid電話會議?

相關問題