Add a public API to allow user to invalidate URLSession used in SDWebImageDownloader to avoid memory leak on non-singleton instance

This commit is contained in:
DreamPiggy 2017-11-27 00:08:50 +08:00
parent 5872bd660b
commit ae96351439
2 changed files with 15 additions and 0 deletions

View File

@ -246,4 +246,11 @@ typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterB
*/
- (void)createNewSessionWithConfiguration:(nonnull NSURLSessionConfiguration *)sessionConfiguration;
/**
* Invalidates the managed session, optionally canceling pending operations.
* If you use custom downloader instead of shared downloader, you need call this method when you do not use it to avoid memory leak
* @param cancelPendingOperations Whether or not to cancel pending operations.
*/
- (void)invalidateSessionAndCancel:(BOOL)cancelPendingOperations;
@end

View File

@ -107,6 +107,14 @@
delegateQueue:nil];
}
- (void)invalidateSessionAndCancel:(BOOL)cancelPendingOperations {
if (cancelPendingOperations) {
[self.session invalidateAndCancel];
} else {
[self.session finishTasksAndInvalidate];
}
}
- (void)dealloc {
[self.session invalidateAndCancel];
self.session = nil;