Skip to content

Commit 247cbcf

Browse files
authored
Merge pull request SDWebImage#3479 from dreampiggy/bugfix/fix_async_block_operation_race
Try to fix the SDAsyncBlockOperation's race condition
2 parents a052164 + 0296901 commit 247cbcf

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

SDWebImage/Core/SDWebImageDownloaderOperation.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ - (void)setExecuting:(BOOL)executing {
334334
[self didChangeValueForKey:@"isExecuting"];
335335
}
336336

337-
- (BOOL)isConcurrent {
337+
- (BOOL)isAsynchronous {
338338
return YES;
339339
}
340340

SDWebImage/Private/SDAsyncBlockOperation.m

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#import "SDAsyncBlockOperation.h"
10+
#import "SDInternalMacros.h"
1011

1112
@interface SDAsyncBlockOperation ()
1213

@@ -38,16 +39,17 @@ - (void)start {
3839
self.finished = YES;
3940
return;
4041
}
41-
4242
self.finished = NO;
4343
self.executing = YES;
44-
45-
if (self.executionBlock) {
46-
self.executionBlock(self);
47-
} else {
48-
self.executing = NO;
49-
self.finished = YES;
50-
}
44+
}
45+
SDAsyncBlock executionBlock = self.executionBlock;
46+
if (executionBlock) {
47+
@weakify(self);
48+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
49+
@strongify(self);
50+
if (!self) return;
51+
executionBlock(self);
52+
});
5153
}
5254
}
5355

@@ -69,7 +71,7 @@ - (void)complete {
6971
self.executing = NO;
7072
}
7173
}
72-
}
74+
}
7375

7476
- (void)setFinished:(BOOL)finished {
7577
[self willChangeValueForKey:@"isFinished"];
@@ -83,7 +85,7 @@ - (void)setExecuting:(BOOL)executing {
8385
[self didChangeValueForKey:@"isExecuting"];
8486
}
8587

86-
- (BOOL)isConcurrent {
88+
- (BOOL)isAsynchronous {
8789
return YES;
8890
}
8991

Tests/Tests/SDWebImageTestDownloadOperation.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ - (void)cancel {
3636
}
3737
}
3838

39-
- (BOOL)isConcurrent {
39+
- (BOOL)isAsynchronous {
4040
return YES;
4141
}
4242

0 commit comments

Comments
 (0)