SDWebImage: Completion handler is not called.

I am trying to manage my map marker with sdwebimage but when i go to background and then when i re-call [imageView setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:[Utils getPlaceholderImageName:location_Details]] options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) completion handler is not called, when i tried to debug if (!wself) return; this line is called with wself nil that stopped me to get control of completion block.

UIImageView *imageView=[[UIImageView alloc] init];
                    [imageView setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:[Utils getPlaceholderImageName:location_Details]] options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            long long currentTimestamp = [[NSDate date] timeIntervalSince1970] * MISEC_PER_SEC;
                            currentTimestamp -= (HOWMUCHSTALELOCATIONTOSHOWINMINUTES * MISEC_PER_MIN);

                            if ([[location_Details objectForKey:@"created"] longLongValue] >= currentTimestamp) {

                                marker.icon=[Utils getRoundedMapIcon:image isStale:NO];
                            }else{

                                marker.icon=[Utils getRoundedMapIcon:image isStale:YES];
                            }
                            marker.userData=location_Details;
                            marker.map = self.mapView;
                        });
                    }];

But then when I did this every thing seems to work fine but I am not sure about this, Please help me with this.

if (url) {
        __weak UIImageView *wself = self;
        id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
//            if (!wself) return;
            dispatch_main_sync_safe(^{
                @try {
                    if (image) {
                        wself.image = image;
                        [wself setNeedsLayout];
                    }
                    if (completedBlock && finished) {
                        completedBlock(image, error, cacheType);
                    }
                }
                @catch (NSException *exception) {

                }
                @finally {

                }
//                if (!wself) return;

            });
        }];
        objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 15 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I would like to put an update on this post, we use SDWebImage as our main image lib (our app have millions of users), and this is a recurring problem which ruin the UX.

Basically completion is sometime never called (on first download of the image), but the image does download and go to cache.

It makes the library unreliable…