Merge pull request #1348 from yirenjun/master
Create a strong ref of weakOperation in the entry of The image downlo…
Showing
1 changed file
with
16 additions
and
8 deletions
@@ -180,14 +180,15 @@ | @@ -180,14 +180,15 @@ | ||
180 | downloaderOptions |= SDWebImageDownloaderIgnoreCachedResponse; | 180 | downloaderOptions |= SDWebImageDownloaderIgnoreCachedResponse; |
181 | } | 181 | } |
182 | id <SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) { | 182 | id <SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) { |
183 | - if (weakOperation.isCancelled) { | 183 | + __strong __typeof(weakOperation) strongOperation = weakOperation; |
184 | + if (!strongOperation || strongOperation.isCancelled) { | ||
184 | // Do nothing if the operation was cancelled | 185 | // Do nothing if the operation was cancelled |
185 | // See #699 for more details | 186 | // See #699 for more details |
186 | // if we would call the completedBlock, there could be a race condition between this block and another completedBlock for the same object, so if this one is called second, we will overwrite the new data | 187 | // if we would call the completedBlock, there could be a race condition between this block and another completedBlock for the same object, so if this one is called second, we will overwrite the new data |
187 | } | 188 | } |
188 | else if (error) { | 189 | else if (error) { |
189 | dispatch_main_sync_safe(^{ | 190 | dispatch_main_sync_safe(^{ |
190 | - if (!weakOperation.isCancelled) { | 191 | + if (strongOperation && !strongOperation.isCancelled) { |
191 | completedBlock(nil, error, SDImageCacheTypeNone, finished, url); | 192 | completedBlock(nil, error, SDImageCacheTypeNone, finished, url); |
192 | } | 193 | } |
193 | }); | 194 | }); |
@@ -226,7 +227,7 @@ | @@ -226,7 +227,7 @@ | ||
226 | } | 227 | } |
227 | 228 | ||
228 | dispatch_main_sync_safe(^{ | 229 | dispatch_main_sync_safe(^{ |
229 | - if (!weakOperation.isCancelled) { | 230 | + if (strongOperation && !strongOperation.isCancelled) { |
230 | completedBlock(transformedImage, nil, SDImageCacheTypeNone, finished, url); | 231 | completedBlock(transformedImage, nil, SDImageCacheTypeNone, finished, url); |
231 | } | 232 | } |
232 | }); | 233 | }); |
@@ -238,7 +239,7 @@ | @@ -238,7 +239,7 @@ | ||
238 | } | 239 | } |
239 | 240 | ||
240 | dispatch_main_sync_safe(^{ | 241 | dispatch_main_sync_safe(^{ |
241 | - if (!weakOperation.isCancelled) { | 242 | + if (strongOperation && !strongOperation.isCancelled) { |
242 | completedBlock(downloadedImage, nil, SDImageCacheTypeNone, finished, url); | 243 | completedBlock(downloadedImage, nil, SDImageCacheTypeNone, finished, url); |
243 | } | 244 | } |
244 | }); | 245 | }); |
@@ -247,7 +248,9 @@ | @@ -247,7 +248,9 @@ | ||
247 | 248 | ||
248 | if (finished) { | 249 | if (finished) { |
249 | @synchronized (self.runningOperations) { | 250 | @synchronized (self.runningOperations) { |
250 | - [self.runningOperations removeObject:operation]; | 251 | + if (strongOperation) { |
252 | + [self.runningOperations removeObject:strongOperation]; | ||
253 | + } | ||
251 | } | 254 | } |
252 | } | 255 | } |
253 | }]; | 256 | }]; |
@@ -255,13 +258,17 @@ | @@ -255,13 +258,17 @@ | ||
255 | [subOperation cancel]; | 258 | [subOperation cancel]; |
256 | 259 | ||
257 | @synchronized (self.runningOperations) { | 260 | @synchronized (self.runningOperations) { |
258 | - [self.runningOperations removeObject:weakOperation]; | 261 | + __strong __typeof(weakOperation) strongOperation = weakOperation; |
262 | + if (strongOperation) { | ||
263 | + [self.runningOperations removeObject:strongOperation]; | ||
264 | + } | ||
259 | } | 265 | } |
260 | }; | 266 | }; |
261 | } | 267 | } |
262 | else if (image) { | 268 | else if (image) { |
263 | dispatch_main_sync_safe(^{ | 269 | dispatch_main_sync_safe(^{ |
264 | - if (!weakOperation.isCancelled) { | 270 | + __strong __typeof(weakOperation) strongOperation = weakOperation; |
271 | + if (strongOperation && !strongOperation.isCancelled) { | ||
265 | completedBlock(image, nil, cacheType, YES, url); | 272 | completedBlock(image, nil, cacheType, YES, url); |
266 | } | 273 | } |
267 | }); | 274 | }); |
@@ -272,7 +279,8 @@ | @@ -272,7 +279,8 @@ | ||
272 | else { | 279 | else { |
273 | // Image not in cache and download disallowed by delegate | 280 | // Image not in cache and download disallowed by delegate |
274 | dispatch_main_sync_safe(^{ | 281 | dispatch_main_sync_safe(^{ |
275 | - if (!weakOperation.isCancelled) { | 282 | + __strong __typeof(weakOperation) strongOperation = weakOperation; |
283 | + if (strongOperation && !weakOperation.isCancelled) { | ||
276 | completedBlock(nil, nil, SDImageCacheTypeNone, YES, url); | 284 | completedBlock(nil, nil, SDImageCacheTypeNone, YES, url); |
277 | } | 285 | } |
278 | }); | 286 | }); |
-
Please register or login to post a comment