From f51992cfd619f5cfffe8c0a298bb29c897193bb5 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 30 Apr 2020 19:19:55 +0800 Subject: [PATCH] Mark the `SDAsyncBlockOperation` safe using the lock and correct override method --- SDWebImage/Private/SDAsyncBlockOperation.m | 58 +++++++++++++++------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/SDWebImage/Private/SDAsyncBlockOperation.m b/SDWebImage/Private/SDAsyncBlockOperation.m index 4aefb4f5..754a2ff5 100644 --- a/SDWebImage/Private/SDAsyncBlockOperation.m +++ b/SDWebImage/Private/SDAsyncBlockOperation.m @@ -35,35 +35,55 @@ } - (void)start { - if (self.isCancelled) { - return; - } - - [self willChangeValueForKey:@"isExecuting"]; - self.executing = YES; - [self didChangeValueForKey:@"isExecuting"]; - - if (self.executionBlock) { - self.executionBlock(self); - } else { - [self complete]; + @synchronized (self) { + if (self.isCancelled) { + self.finished = YES; + return; + } + + [self willChangeValueForKey:@"isExecuting"]; + self.executing = YES; + [self didChangeValueForKey:@"isExecuting"]; + + if (self.executionBlock) { + self.executionBlock(self); + } else { + self.executing = NO; + self.finished = YES; + } } } - (void)cancel { - [super cancel]; - if (self.isExecuting) { - [self complete]; + @synchronized (self) { + [super cancel]; + if (self.isExecuting) { + self.executing = NO; + self.finished = YES; + } } } + - (void)complete { - [self willChangeValueForKey:@"isExecuting"]; + @synchronized (self) { + if (self.isExecuting) { + self.executing = NO; + self.finished = YES; + } + } + } + +- (void)setFinished:(BOOL)finished { [self willChangeValueForKey:@"isFinished"]; - self.executing = NO; - self.finished = YES; - [self didChangeValueForKey:@"isExecuting"]; + _finished = finished; [self didChangeValueForKey:@"isFinished"]; } +- (void)setExecuting:(BOOL)executing { + [self willChangeValueForKey:@"isExecuting"]; + _executing = executing; + [self didChangeValueForKey:@"isExecuting"]; +} + @end