From 3d3471e4907e99e4c03962186b939ed5ef37b0be Mon Sep 17 00:00:00 2001 From: Naoki Morita Date: Tue, 16 Dec 2014 10:52:34 +0900 Subject: [PATCH] Fix NSNotificationCenter dispatch on subthreads. --- SDWebImage/SDWebImageDownloaderOperation.m | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index 0aefae30..e7bafc96 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -95,7 +95,9 @@ 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 @@ -150,7 +152,9 @@ 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. @@ -216,8 +220,9 @@ } 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); @@ -340,7 +345,9 @@ 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]) { @@ -377,7 +384,9 @@ 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) {