From 181d367215aedf6e0ed423215682c4f7ea9af573 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Thu, 11 Oct 2018 17:44:18 +0800 Subject: [PATCH] Fix background download operation FIx background download Add task check when operation will deallocated && tidy code --- SDWebImage/SDWebImageDownloaderOperation.m | 49 +++++++++++----------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index e0a68976..69887e2f 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -64,6 +64,11 @@ typedef NSMutableDictionary SDCallbacksDictionary; @synthesize executing = _executing; @synthesize finished = _finished; +- (void)dealloc { + // Edge case if user call [SDWebImageDownloaderOperation start] directly and deallocated it. + [self cancel]; +} + - (nonnull instancetype)init { return [self initWithRequest:nil inSession:nil options:0]; } @@ -82,6 +87,9 @@ typedef NSMutableDictionary SDCallbacksDictionary; _unownedSession = session; _callbacksLock = dispatch_semaphore_create(1); _coderQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderOperationCoderQueue", DISPATCH_QUEUE_SERIAL); +#if SD_UIKIT + _backgroundTaskId = UIBackgroundTaskInvalid; +#endif } return self; } @@ -135,14 +143,7 @@ typedef NSMutableDictionary SDCallbacksDictionary; __weak __typeof__ (self) wself = self; UIApplication * app = [UIApplicationClass performSelector:@selector(sharedApplication)]; self.backgroundTaskId = [app beginBackgroundTaskWithExpirationHandler:^{ - __strong __typeof (wself) sself = wself; - - if (sself) { - [sself cancel]; - - [app endBackgroundTask:sself.backgroundTaskId]; - sself.backgroundTaskId = UIBackgroundTaskInvalid; - } + [wself cancel]; }]; } #endif @@ -206,18 +207,6 @@ typedef NSMutableDictionary SDCallbacksDictionary; [self done]; return; } - -#if SD_UIKIT - Class UIApplicationClass = NSClassFromString(@"UIApplication"); - if(!UIApplicationClass || ![UIApplicationClass respondsToSelector:@selector(sharedApplication)]) { - return; - } - if (self.backgroundTaskId != UIBackgroundTaskInvalid) { - UIApplication * app = [UIApplication performSelector:@selector(sharedApplication)]; - [app endBackgroundTask:self.backgroundTaskId]; - self.backgroundTaskId = UIBackgroundTaskInvalid; - } -#endif } - (void)cancel { @@ -256,11 +245,23 @@ typedef NSMutableDictionary SDCallbacksDictionary; LOCK(self.callbacksLock); [self.callbackBlocks removeAllObjects]; UNLOCK(self.callbacksLock); - self.dataTask = nil; - if (self.ownedSession) { - [self.ownedSession invalidateAndCancel]; - self.ownedSession = nil; + @synchronized (self) { + self.dataTask = nil; + + if (self.ownedSession) { + [self.ownedSession invalidateAndCancel]; + self.ownedSession = nil; + } + +#if SD_UIKIT + if (self.backgroundTaskId != UIBackgroundTaskInvalid) { + // If backgroundTaskId != UIBackgroundTaskInvalid, sharedApplication is always exist + UIApplication * app = [UIApplication performSelector:@selector(sharedApplication)]; + [app endBackgroundTask:self.backgroundTaskId]; + self.backgroundTaskId = UIBackgroundTaskInvalid; + } +#endif } }