Skip to content

Commit

Permalink
Merge pull request SDWebImage#987 from wantedly/fix-notification-disp…
Browse files Browse the repository at this point in the history
…atch

Fix NSNotificationCenter dispatch on subthreads.
  • Loading branch information
bpoplauschi committed Jan 7, 2015
2 parents 21656fa + 3d3471e commit feca1e8
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions SDWebImage/SDWebImageDownloaderOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ - (void)start {
if (self.progressBlock) {
self.progressBlock(0, NSURLResponseUnknownLength);
}
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:self];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:self];
});

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_5_1) {
// Make sure to run the runloop in our background thread so it can process downloaded data
Expand Down Expand Up @@ -150,7 +152,9 @@ - (void)cancelInternal {

if (self.connection) {
[self.connection cancel];
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
});

// As we cancelled the connection, its callback won't be called and thus won't
// maintain the isFinished and isExecuting flags.
Expand Down Expand Up @@ -216,8 +220,9 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon
} else {
[self.connection cancel];
}

[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
});

if (self.completedBlock) {
self.completedBlock(nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:[((NSHTTPURLResponse *)response) statusCode] userInfo:nil], YES);
Expand Down Expand Up @@ -340,7 +345,9 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {
CFRunLoopStop(CFRunLoopGetCurrent());
self.thread = nil;
self.connection = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
});
}

if (![[NSURLCache sharedURLCache] cachedResponseForRequest:_request]) {
Expand Down Expand Up @@ -377,7 +384,9 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)err
CFRunLoopStop(CFRunLoopGetCurrent());
self.thread = nil;
self.connection = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
});
}

if (self.completedBlock) {
Expand Down

0 comments on commit feca1e8

Please sign in to comment.