Skip to content

Commit 3d3471e

Browse files
author
Naoki Morita
committed
Fix NSNotificationCenter dispatch on subthreads.
1 parent 6698910 commit 3d3471e

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

SDWebImage/SDWebImageDownloaderOperation.m

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ - (void)start {
9595
if (self.progressBlock) {
9696
self.progressBlock(0, NSURLResponseUnknownLength);
9797
}
98-
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:self];
98+
dispatch_async(dispatch_get_main_queue(), ^{
99+
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:self];
100+
});
99101

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

151153
if (self.connection) {
152154
[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+
});
154158

155159
// As we cancelled the connection, its callback won't be called and thus won't
156160
// maintain the isFinished and isExecuting flags.
@@ -216,8 +220,9 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon
216220
} else {
217221
[self.connection cancel];
218222
}
219-
220-
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
223+
dispatch_async(dispatch_get_main_queue(), ^{
224+
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
225+
});
221226

222227
if (self.completedBlock) {
223228
self.completedBlock(nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:[((NSHTTPURLResponse *)response) statusCode] userInfo:nil], YES);
@@ -340,7 +345,9 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {
340345
CFRunLoopStop(CFRunLoopGetCurrent());
341346
self.thread = nil;
342347
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+
});
344351
}
345352

346353
if (![[NSURLCache sharedURLCache] cachedResponseForRequest:_request]) {
@@ -377,7 +384,9 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)err
377384
CFRunLoopStop(CFRunLoopGetCurrent());
378385
self.thread = nil;
379386
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+
});
381390
}
382391

383392
if (self.completedBlock) {

0 commit comments

Comments
 (0)