Skip to content

Commit

Permalink
Try to fix the thread block (SDWebImage#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Poitrey authored and devedup committed Sep 10, 2014
1 parent 1755b4c commit a0d659b
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions SDWebImage/SDWebImageDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ - (NSInteger)maxConcurrentDownloads

- (id<SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(void (^)(NSUInteger, long long))progressBlock completed:(void (^)(UIImage *, NSData *, NSError *, BOOL))completedBlock
{
__block SDWebImageDownloaderOperation *blockOperation;
__block SDWebImageDownloaderOperation *operation;
__weak SDWebImageDownloader *wself = self;

[self addProgressCallback:progressBlock andCompletedBlock:completedBlock forURL:url createCallback:^
Expand All @@ -121,7 +121,7 @@ - (NSInteger)maxConcurrentDownloads
request.HTTPShouldHandleCookies = NO;
request.HTTPShouldUsePipelining = YES;
request.allHTTPHeaderFields = wself.HTTPHeaders;
blockOperation = [SDWebImageDownloaderOperation.alloc initWithRequest:request options:options progress:^(NSUInteger receivedSize, long long expectedSize)
operation = [SDWebImageDownloaderOperation.alloc initWithRequest:request options:options progress:^(NSUInteger receivedSize, long long expectedSize)
{
if (!wself) return;
SDWebImageDownloader *sself = wself;
Expand Down Expand Up @@ -153,17 +153,15 @@ - (NSInteger)maxConcurrentDownloads
SDWebImageDownloader *sself = wself;
[sself removeCallbacksForURL:url];
}];
[wself.downloadQueue addOperation:blockOperation];
[wself.downloadQueue addOperation:operation];
if (wself.executionOrder == SDWebImageDownloaderLIFOExecutionOrder)
{
// Emulate LIFO execution order by systematically adding new operations as last operation's dependency
[wself.lastAddedOperation addDependency:blockOperation];
wself.lastAddedOperation = blockOperation;
[wself.lastAddedOperation addDependency:operation];
wself.lastAddedOperation = operation;
}
}];

id operation = blockOperation;
blockOperation = nil; // break retain cycle
return operation;
}

Expand Down

0 comments on commit a0d659b

Please sign in to comment.