Ответ 1
При создании объекта 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.
*/