From f37474d5c8be8872a3e64292df0fdc93f2a1a751 Mon Sep 17 00:00:00 2001 From: Gabriele Petronella Date: Thu, 29 Aug 2013 12:46:15 +0100 Subject: [PATCH 1/2] Enforced presence of a completedBlock in downloadWithURL:options:progress:completed: This method bails out in case of a missing `completedBlock`. While this makes sense (downloading the image without a completion block and not doing anything with it is pointless), a client passing a empty block is not informed about the mistake. `NSParameterAssert` validates the input and it informs the client about bad usage of the API. --- SDWebImage/SDWebImageManager.m | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index 7e241377..8940d656 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -68,6 +68,9 @@ - (id)downloadWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedWithFinishedBlock)completedBlock { + // Invoking this method without a completedBlock is pointless + NSParameterAssert(completedBlock); + // Very common mistake is to send the URL using NSString object instead of NSURL. For some strange reason, XCode won't // throw any warning for this type mismatch. Here we failsafe this error by allowing URLs to be passed as NSString. if ([url isKindOfClass:NSString.class]) @@ -90,16 +93,13 @@ isFailedUrl = [self.failedURLs containsObject:url]; } - if (!url || !completedBlock || (!(options & SDWebImageRetryFailed) && isFailedUrl)) + if (!url || (!(options & SDWebImageRetryFailed) && isFailedUrl)) { - if (completedBlock) + dispatch_main_sync_safe(^ { - dispatch_main_sync_safe(^ - { NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]; completedBlock(nil, error, SDImageCacheTypeNone, YES); - }); - } + }); return operation; } From 5bbddae80dda51dd2b0bbee9660fca0572d3f0d5 Mon Sep 17 00:00:00 2001 From: Gabriele Petronella Date: Thu, 29 Aug 2013 13:03:33 +0100 Subject: [PATCH 2/2] Updated doc marking the completedBlock parameter of downloadWithURL:options:progress:completed as required --- SDWebImage/SDWebImageManager.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/SDWebImage/SDWebImageManager.h b/SDWebImage/SDWebImageManager.h index 736fbba8..da8abcec 100644 --- a/SDWebImage/SDWebImageManager.h +++ b/SDWebImage/SDWebImageManager.h @@ -142,7 +142,9 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager]; * @param progressBlock A block called while image is downloading * @param completedBlock A block called when operation has been completed. * - * This block as no return value and takes the requested UIImage as first parameter. + * This parameter is required. + * + * This block has no return value and takes the requested UIImage as first parameter. * In case of error the image parameter is nil and the second parameter may contain an NSError. * * The third parameter is an `SDImageCacheType` enum indicating if the image was retrived from the local cache