Merge pull request #3205 from dreampiggy/bugfix_session_invalid

Ensure the invalid URLSeesion will receive the cancel callback and does not trigger the any exception from framework
This commit is contained in:
DreamPiggy 2021-04-08 14:44:01 +08:00 committed by GitHub
commit 15e16e9e7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View File

@ -140,11 +140,12 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext;
}
- (void)dealloc {
[self.session invalidateAndCancel];
self.session = nil;
[self.downloadQueue cancelAllOperations];
[self.config removeObserver:self forKeyPath:NSStringFromSelector(@selector(maxConcurrentDownloads)) context:SDWebImageDownloaderContext];
// Invalide the URLSession after all operations been cancelled
[self.session invalidateAndCancel];
self.session = nil;
}
- (void)invalidateSessionAndCancel:(BOOL)cancelPendingOperations {

View File

@ -194,6 +194,13 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
}
}
if (!session.delegate) {
// Session been invalid and has no delegate at all
[self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorInvalidDownloadOperation userInfo:@{NSLocalizedDescriptionKey : @"Session delegate is nil and invalid"}]];
[self reset];
return;
}
self.dataTask = [session dataTaskWithRequest:self.request];
self.executing = YES;
}