Always call the completedBlock from main thread (fix #397)

This is not a good practice but doing otherwise leads to a lot of mistakes
This commit is contained in:
Olivier Poitrey 2013-08-07 21:15:32 -07:00
parent 3a6d9481c9
commit 1e63f33a85
1 changed files with 31 additions and 10 deletions

View File

@ -93,8 +93,11 @@
{
if (completedBlock)
{
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil];
completedBlock(nil, error, SDImageCacheTypeNone, YES);
dispatch_main_sync_safe(^
{
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil];
completedBlock(nil, error, SDImageCacheTypeNone, YES);
});
}
return operation;
}
@ -113,9 +116,12 @@
{
if (image && options & SDWebImageRefreshCached)
{
// If image was found in the cache bug SDWebImageRefreshCached is provided, notify about the cached image
// AND try to re-download it in order to let a chance to NSURLCache to refresh it from server.
completedBlock(image, nil, cacheType, YES);
dispatch_main_sync_safe(^
{
// If image was found in the cache bug SDWebImageRefreshCached is provided, notify about the cached image
// AND try to re-download it in order to let a chance to NSURLCache to refresh it from server.
completedBlock(image, nil, cacheType, YES);
});
}
// download if no image or requested to refresh anyway, and download allowed by delegate
@ -134,11 +140,17 @@
{
if (weakOperation.cancelled)
{
completedBlock(nil, nil, SDImageCacheTypeNone, finished);
dispatch_main_sync_safe(^
{
completedBlock(nil, nil, SDImageCacheTypeNone, finished);
});
}
else if (error)
{
completedBlock(nil, error, SDImageCacheTypeNone, finished);
dispatch_main_sync_safe(^
{
completedBlock(nil, error, SDImageCacheTypeNone, finished);
});
if (error.code != NSURLErrorNotConnectedToInternet)
{
@ -177,7 +189,10 @@
}
else
{
completedBlock(downloadedImage, nil, SDImageCacheTypeNone, finished);
dispatch_main_sync_safe(^
{
completedBlock(downloadedImage, nil, SDImageCacheTypeNone, finished);
});
if (downloadedImage && finished)
{
@ -198,7 +213,10 @@
}
else if (image)
{
completedBlock(image, nil, cacheType, YES);
dispatch_main_sync_safe(^
{
completedBlock(image, nil, cacheType, YES);
});
@synchronized(self.runningOperations)
{
[self.runningOperations removeObject:operation];
@ -207,7 +225,10 @@
else
{
// Image not in cache and download disallowed by delegate
completedBlock(nil, nil, SDImageCacheTypeNone, YES);
dispatch_main_sync_safe(^
{
completedBlock(nil, nil, SDImageCacheTypeNone, YES);
});
@synchronized(self.runningOperations)
{
[self.runningOperations removeObject:operation];