Skip to content

Commit

Permalink
Returning error in setImage completedBlock if the url was nil. Added …
Browse files Browse the repository at this point in the history
…`dispatch_main_async_safe` macro. Fixes #505
  • Loading branch information
bpoplauschi committed Jun 19, 2014
1 parent eb91fdd commit af3e4f8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
7 changes: 7 additions & 0 deletions SDWebImage/MKAnnotationView+WebCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
});
}];
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} else {
dispatch_main_async_safe(^{
NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
if (completedBlock) {
completedBlock(nil, error, SDImageCacheTypeNone, url);
}
});
}
}

Expand Down
10 changes: 8 additions & 2 deletions SDWebImage/SDWebImageCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ extern UIImage *SDScaledImageForKey(NSString *key, UIImage *image);
#define dispatch_main_sync_safe(block)\
if ([NSThread isMainThread]) {\
block();\
}\
else {\
} else {\
dispatch_sync(dispatch_get_main_queue(), block);\
}

#define dispatch_main_async_safe(block)\
if ([NSThread isMainThread]) {\
block();\
} else {\
dispatch_async(dispatch_get_main_queue(), block);\
}
17 changes: 15 additions & 2 deletions SDWebImage/UIButton+WebCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ @implementation UIButton (WebCache)
- (NSURL *)currentImageURL {
NSURL *url = self.imageURLStorage[@(self.state)];

if (!url)
{
if (!url) {
url = self.imageURLStorage[@(UIControlStateNormal)];
}

Expand Down Expand Up @@ -57,6 +56,13 @@ - (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state placehold
if (!url) {
[self.imageURLStorage removeObjectForKey:@(state)];

dispatch_main_async_safe(^{
NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
if (completedBlock) {
completedBlock(nil, error, SDImageCacheTypeNone, url);
}
});

return;
}

Expand Down Expand Up @@ -120,6 +126,13 @@ - (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state
});
}];
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} else {
dispatch_main_async_safe(^{
NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
if (completedBlock) {
completedBlock(nil, error, SDImageCacheTypeNone, url);
}
});
}
}

Expand Down
7 changes: 7 additions & 0 deletions SDWebImage/UIImageView+HighlightedWebCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)op
});
}];
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} else {
dispatch_main_async_safe(^{
NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
if (completedBlock) {
completedBlock(nil, error, SDImageCacheTypeNone, url);
}
});
}
}

Expand Down
7 changes: 7 additions & 0 deletions SDWebImage/UIImageView+WebCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
});
}];
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} else {
dispatch_main_async_safe(^{
NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
if (completedBlock) {
completedBlock(nil, error, SDImageCacheTypeNone, url);
}
});
}
}

Expand Down

0 comments on commit af3e4f8

Please sign in to comment.