Search code examples
iosobjective-ccocoa-touchsdwebimage

SDWebImage eager load images


I am trying to eager load bunch of images:

for (NSDictionary *s in things) {
    [manager downloadWithURL:[NSURL URLWithString:s[photo]]
                     options:0
                    progress:nil
                   completed:nil];
}

It's not downloading these images. However, if I pass in an empty completion block, like so:

for (NSDictionary *s in things) {
    [manager downloadWithURL:[NSURL URLWithString:s[photo]]
                     options:0
                    progress:nil
                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { }];
}

then it works just fine. My question is: why? Is there a better way to do this? Passing in an empty block doesn't seem right to me.


Solution

  • The API you are using is not the correct one.

    To prefetch images and store them in cache, use SDWebImagePrefetcher which is meant for that.

    NSMutableArray * urls = [NSMutableArray arrayWithCapacity:things.count];
    for (NSDictionary *s in things) {
        [urls addObject:[NSURL URLWithString:s[photo]]];
    }
    [[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:urls];
    

    As a side note I submitted a pull request - which has just been merged - to enforce the presence of a completedBlock in the API you are (mis)using, so that other programmers won't fall you in your same mistake.