Merge pull request #2500 from zhongwuzw/fix-background-download
Fix background download
This commit is contained in:
commit
38a9222401
|
@ -88,6 +88,9 @@ typedef NSMutableDictionary<NSString *, id> 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;
|
||||
}
|
||||
|
@ -141,14 +144,7 @@ typedef NSMutableDictionary<NSString *, id> 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
|
||||
|
@ -212,18 +208,6 @@ typedef NSMutableDictionary<NSString *, id> 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 {
|
||||
|
@ -262,12 +246,24 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
|
|||
SD_LOCK(self.callbacksLock);
|
||||
[self.callbackBlocks removeAllObjects];
|
||||
SD_UNLOCK(self.callbacksLock);
|
||||
|
||||
@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
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setFinished:(BOOL)finished {
|
||||
|
|
Loading…
Reference in New Issue