Skip to content

Commit

Permalink
Ensure the invalid URLSeesion will receive the cancel callback and do…
Browse files Browse the repository at this point in the history
…es not trigger the any exception from framework
  • Loading branch information
dreampiggy committed Apr 6, 2021
1 parent d1c75d8 commit 90c0906
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 4 additions & 3 deletions SDWebImage/Core/SDWebImageDownloader.m
Expand Up @@ -140,11 +140,12 @@ - (instancetype)initWithConfig:(SDWebImageDownloaderConfig *)config {
}

- (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 {
Expand Down
7 changes: 7 additions & 0 deletions SDWebImage/Core/SDWebImageDownloaderOperation.m
Expand Up @@ -194,6 +194,13 @@ - (void)start {
}
}

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;
}
Expand Down

0 comments on commit 90c0906

Please sign in to comment.