Improved readability in `SDWebImageManager` by creating `safelyRemoveOperationFromRunning:` method
Showing
1 changed file
with
14 additions
and
22 deletions
@@ -145,10 +145,7 @@ | @@ -145,10 +145,7 @@ | ||
145 | 145 | ||
146 | operation.cacheOperation = [self.imageCache queryCacheOperationForKey:key done:^(UIImage *cachedImage, NSData *cachedData, SDImageCacheType cacheType) { | 146 | operation.cacheOperation = [self.imageCache queryCacheOperationForKey:key done:^(UIImage *cachedImage, NSData *cachedData, SDImageCacheType cacheType) { |
147 | if (operation.isCancelled) { | 147 | if (operation.isCancelled) { |
148 | - @synchronized (self.runningOperations) { | ||
149 | - [self.runningOperations removeObject:operation]; | ||
150 | - } | ||
151 | - | 148 | + [self safelyRemoveOperationFromRunning:operation]; |
152 | return; | 149 | return; |
153 | } | 150 | } |
154 | 151 | ||
@@ -242,22 +239,13 @@ | @@ -242,22 +239,13 @@ | ||
242 | } | 239 | } |
243 | 240 | ||
244 | if (finished) { | 241 | if (finished) { |
245 | - @synchronized (self.runningOperations) { | ||
246 | - if (strongOperation) { | ||
247 | - [self.runningOperations removeObject:strongOperation]; | ||
248 | - } | ||
249 | - } | 242 | + [self safelyRemoveOperationFromRunning:strongOperation]; |
250 | } | 243 | } |
251 | }]; | 244 | }]; |
252 | operation.cancelBlock = ^{ | 245 | operation.cancelBlock = ^{ |
253 | [self.imageDownloader cancel:subOperation]; | 246 | [self.imageDownloader cancel:subOperation]; |
254 | - | ||
255 | - @synchronized (self.runningOperations) { | ||
256 | __strong __typeof(weakOperation) strongOperation = weakOperation; | 247 | __strong __typeof(weakOperation) strongOperation = weakOperation; |
257 | - if (strongOperation) { | ||
258 | - [self.runningOperations removeObject:strongOperation]; | ||
259 | - } | ||
260 | - } | 248 | + [self safelyRemoveOperationFromRunning:strongOperation]; |
261 | }; | 249 | }; |
262 | } else if (cachedImage) { | 250 | } else if (cachedImage) { |
263 | dispatch_main_async_safe(^{ | 251 | dispatch_main_async_safe(^{ |
@@ -266,9 +254,7 @@ | @@ -266,9 +254,7 @@ | ||
266 | completedBlock(cachedImage, cachedData, nil, cacheType, YES, url); | 254 | completedBlock(cachedImage, cachedData, nil, cacheType, YES, url); |
267 | } | 255 | } |
268 | }); | 256 | }); |
269 | - @synchronized (self.runningOperations) { | ||
270 | - [self.runningOperations removeObject:operation]; | ||
271 | - } | 257 | + [self safelyRemoveOperationFromRunning:operation]; |
272 | } else { | 258 | } else { |
273 | // Image not in cache and download disallowed by delegate | 259 | // Image not in cache and download disallowed by delegate |
274 | dispatch_main_async_safe(^{ | 260 | dispatch_main_async_safe(^{ |
@@ -277,9 +263,7 @@ | @@ -277,9 +263,7 @@ | ||
277 | completedBlock(nil, nil, nil, SDImageCacheTypeNone, YES, url); | 263 | completedBlock(nil, nil, nil, SDImageCacheTypeNone, YES, url); |
278 | } | 264 | } |
279 | }); | 265 | }); |
280 | - @synchronized (self.runningOperations) { | ||
281 | - [self.runningOperations removeObject:operation]; | ||
282 | - } | 266 | + [self safelyRemoveOperationFromRunning:operation]; |
283 | } | 267 | } |
284 | }]; | 268 | }]; |
285 | 269 | ||
@@ -303,12 +287,20 @@ | @@ -303,12 +287,20 @@ | ||
303 | 287 | ||
304 | - (BOOL)isRunning { | 288 | - (BOOL)isRunning { |
305 | BOOL isRunning = NO; | 289 | BOOL isRunning = NO; |
306 | - @synchronized(self.runningOperations) { | 290 | + @synchronized (self.runningOperations) { |
307 | isRunning = (self.runningOperations.count > 0); | 291 | isRunning = (self.runningOperations.count > 0); |
308 | } | 292 | } |
309 | return isRunning; | 293 | return isRunning; |
310 | } | 294 | } |
311 | 295 | ||
296 | +- (void)safelyRemoveOperationFromRunning:(nullable SDWebImageCombinedOperation*)operation { | ||
297 | + @synchronized (self.runningOperations) { | ||
298 | + if (operation) { | ||
299 | + [self.runningOperations removeObject:operation]; | ||
300 | + } | ||
301 | + } | ||
302 | +} | ||
303 | + | ||
312 | @end | 304 | @end |
313 | 305 | ||
314 | 306 |
-
Please register or login to post a comment