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-06 15:00:52 +08:00
parent 3fcf40d12f
commit d452afdb54
2 changed files with 11 additions and 3 deletions

View File

@ -140,11 +140,12 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext;
} }
- (void)dealloc { - (void)dealloc {
[self.session invalidateAndCancel];
self.session = nil;
[self.downloadQueue cancelAllOperations]; [self.downloadQueue cancelAllOperations];
[self.config removeObserver:self forKeyPath:NSStringFromSelector(@selector(maxConcurrentDownloads)) context:SDWebImageDownloaderContext]; [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 { - (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.dataTask = [session dataTaskWithRequest:self.request];
self.executing = YES; self.executing = YES;
} }