Fix NSNotificationCenter dispatch on subthreads.

This commit is contained in:
Naoki Morita 2014-12-16 10:52:34 +09:00
parent 6698910fe1
commit 3d3471e490
1 changed files with 15 additions and 6 deletions

View File

@ -95,7 +95,9 @@
if (self.progressBlock) { if (self.progressBlock) {
self.progressBlock(0, NSURLResponseUnknownLength); 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) { if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_5_1) {
// Make sure to run the runloop in our background thread so it can process downloaded data // Make sure to run the runloop in our background thread so it can process downloaded data
@ -150,7 +152,9 @@
if (self.connection) { if (self.connection) {
[self.connection cancel]; [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 // As we cancelled the connection, its callback won't be called and thus won't
// maintain the isFinished and isExecuting flags. // maintain the isFinished and isExecuting flags.
@ -216,8 +220,9 @@
} else { } else {
[self.connection cancel]; [self.connection cancel];
} }
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
});
if (self.completedBlock) { if (self.completedBlock) {
self.completedBlock(nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:[((NSHTTPURLResponse *)response) statusCode] userInfo:nil], YES); self.completedBlock(nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:[((NSHTTPURLResponse *)response) statusCode] userInfo:nil], YES);
@ -340,7 +345,9 @@
CFRunLoopStop(CFRunLoopGetCurrent()); CFRunLoopStop(CFRunLoopGetCurrent());
self.thread = nil; self.thread = nil;
self.connection = 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]) { if (![[NSURLCache sharedURLCache] cachedResponseForRequest:_request]) {
@ -377,7 +384,9 @@
CFRunLoopStop(CFRunLoopGetCurrent()); CFRunLoopStop(CFRunLoopGetCurrent());
self.thread = nil; self.thread = nil;
self.connection = 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) { if (self.completedBlock) {