Skip to content

Commit

Permalink
Fix background download operation
Browse files Browse the repository at this point in the history
FIx background download

Add task check when operation will deallocated && tidy code

Tidy code further

Tidy further
  • Loading branch information
zhongwuzw committed Dec 26, 2018
1 parent abc2b30 commit 9617a34
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions SDWebImage/SDWebImageDownloaderOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ @implementation SDWebImageDownloaderOperation
@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];
}
Expand All @@ -88,6 +93,9 @@ - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
_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;
}
Expand Down Expand Up @@ -141,14 +149,7 @@ - (void)start {
__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
Expand Down Expand Up @@ -212,18 +213,6 @@ - (void)start {
[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 {
Expand Down Expand Up @@ -262,11 +251,23 @@ - (void)reset {
SD_LOCK(self.callbacksLock);
[self.callbackBlocks removeAllObjects];
SD_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
}
}

Expand Down

0 comments on commit 9617a34

Please sign in to comment.