Merge pull request #2570 from dreampiggy/fix-background-download-4.x

Fix background download 4.x
This commit is contained in:
Wu Zhong 2018-12-28 16:00:15 +08:00 committed by GitHub
commit 6026cade7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 24 deletions

View File

@ -82,6 +82,9 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
_unownedSession = session; _unownedSession = session;
_callbacksLock = dispatch_semaphore_create(1); _callbacksLock = dispatch_semaphore_create(1);
_coderQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderOperationCoderQueue", DISPATCH_QUEUE_SERIAL); _coderQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderOperationCoderQueue", DISPATCH_QUEUE_SERIAL);
#if SD_UIKIT
_backgroundTaskId = UIBackgroundTaskInvalid;
#endif
} }
return self; return self;
} }
@ -135,14 +138,7 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
__weak __typeof__ (self) wself = self; __weak __typeof__ (self) wself = self;
UIApplication * app = [UIApplicationClass performSelector:@selector(sharedApplication)]; UIApplication * app = [UIApplicationClass performSelector:@selector(sharedApplication)];
self.backgroundTaskId = [app beginBackgroundTaskWithExpirationHandler:^{ self.backgroundTaskId = [app beginBackgroundTaskWithExpirationHandler:^{
__strong __typeof (wself) sself = wself; [wself cancel];
if (sself) {
[sself cancel];
[app endBackgroundTask:sself.backgroundTaskId];
sself.backgroundTaskId = UIBackgroundTaskInvalid;
}
}]; }];
} }
#endif #endif
@ -206,18 +202,6 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
[self done]; [self done];
return; 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 { - (void)cancel {
@ -256,11 +240,23 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
LOCK(self.callbacksLock); LOCK(self.callbacksLock);
[self.callbackBlocks removeAllObjects]; [self.callbackBlocks removeAllObjects];
UNLOCK(self.callbacksLock); UNLOCK(self.callbacksLock);
self.dataTask = nil;
if (self.ownedSession) { @synchronized (self) {
[self.ownedSession invalidateAndCancel]; self.dataTask = nil;
self.ownedSession = 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
} }
} }