Authored by Bogdan Poplauschi

Merge pull request #987 from wantedly/fix-notification-dispatch

Fix NSNotificationCenter dispatch on subthreads.
@@ -95,7 +95,9 @@ @@ -95,7 +95,9 @@
95 if (self.progressBlock) { 95 if (self.progressBlock) {
96 self.progressBlock(0, NSURLResponseUnknownLength); 96 self.progressBlock(0, NSURLResponseUnknownLength);
97 } 97 }
98 - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:self]; 98 + dispatch_async(dispatch_get_main_queue(), ^{
  99 + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:self];
  100 + });
99 101
100 if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_5_1) { 102 if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_5_1) {
101 // Make sure to run the runloop in our background thread so it can process downloaded data 103 // Make sure to run the runloop in our background thread so it can process downloaded data
@@ -150,7 +152,9 @@ @@ -150,7 +152,9 @@
150 152
151 if (self.connection) { 153 if (self.connection) {
152 [self.connection cancel]; 154 [self.connection cancel];
153 - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self]; 155 + dispatch_async(dispatch_get_main_queue(), ^{
  156 + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
  157 + });
154 158
155 // As we cancelled the connection, its callback won't be called and thus won't 159 // As we cancelled the connection, its callback won't be called and thus won't
156 // maintain the isFinished and isExecuting flags. 160 // maintain the isFinished and isExecuting flags.
@@ -216,8 +220,9 @@ @@ -216,8 +220,9 @@
216 } else { 220 } else {
217 [self.connection cancel]; 221 [self.connection cancel];
218 } 222 }
219 -  
220 - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; 223 + dispatch_async(dispatch_get_main_queue(), ^{
  224 + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
  225 + });
221 226
222 if (self.completedBlock) { 227 if (self.completedBlock) {
223 self.completedBlock(nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:[((NSHTTPURLResponse *)response) statusCode] userInfo:nil], YES); 228 self.completedBlock(nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:[((NSHTTPURLResponse *)response) statusCode] userInfo:nil], YES);
@@ -340,7 +345,9 @@ @@ -340,7 +345,9 @@
340 CFRunLoopStop(CFRunLoopGetCurrent()); 345 CFRunLoopStop(CFRunLoopGetCurrent());
341 self.thread = nil; 346 self.thread = nil;
342 self.connection = nil; 347 self.connection = nil;
343 - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; 348 + dispatch_async(dispatch_get_main_queue(), ^{
  349 + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
  350 + });
344 } 351 }
345 352
346 if (![[NSURLCache sharedURLCache] cachedResponseForRequest:_request]) { 353 if (![[NSURLCache sharedURLCache] cachedResponseForRequest:_request]) {
@@ -377,7 +384,9 @@ @@ -377,7 +384,9 @@
377 CFRunLoopStop(CFRunLoopGetCurrent()); 384 CFRunLoopStop(CFRunLoopGetCurrent());
378 self.thread = nil; 385 self.thread = nil;
379 self.connection = nil; 386 self.connection = nil;
380 - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; 387 + dispatch_async(dispatch_get_main_queue(), ^{
  388 + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
  389 + });
381 } 390 }
382 391
383 if (self.completedBlock) { 392 if (self.completedBlock) {