Merge pull request #2199 from dreampiggy/performance_decode_in_operation_queue
Decode the image in the operation level's queue instead of URLSession delegate queue
This commit is contained in:
commit
1986669d49
|
@ -63,7 +63,7 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification
|
|||
@property (nonatomic, assign) BOOL shouldUseCredentialStorage __deprecated_msg("Property deprecated. Does nothing. Kept only for backwards compatibility");
|
||||
|
||||
/**
|
||||
* The credential used for authentication challenges in `-connection:didReceiveAuthenticationChallenge:`.
|
||||
* The credential used for authentication challenges in `-URLSession:task:didReceiveChallenge:completionHandler:`.
|
||||
*
|
||||
* This will be overridden by any shared credentials that exist for the username or password of the request URL, if present.
|
||||
*/
|
||||
|
@ -80,7 +80,7 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification
|
|||
@property (assign, nonatomic) NSInteger expectedSize;
|
||||
|
||||
/**
|
||||
* The response returned by the operation's connection.
|
||||
* The response returned by the operation's task.
|
||||
*/
|
||||
@property (strong, nonatomic, nullable) NSURLResponse *response;
|
||||
|
||||
|
|
|
@ -11,6 +11,9 @@
|
|||
#import "NSImage+WebCache.h"
|
||||
#import "SDWebImageCodersManager.h"
|
||||
|
||||
#define LOCK(lock) dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER);
|
||||
#define UNLOCK(lock) dispatch_semaphore_signal(lock);
|
||||
|
||||
NSString *const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification";
|
||||
NSString *const SDWebImageDownloadReceiveResponseNotification = @"SDWebImageDownloadReceiveResponseNotification";
|
||||
NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification";
|
||||
|
@ -28,7 +31,7 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
|
|||
@property (assign, nonatomic, getter = isExecuting) BOOL executing;
|
||||
@property (assign, nonatomic, getter = isFinished) BOOL finished;
|
||||
@property (strong, nonatomic, nullable) NSMutableData *imageData;
|
||||
@property (copy, nonatomic, nullable) NSData *cachedData;
|
||||
@property (copy, nonatomic, nullable) NSData *cachedData; // for `SDWebImageDownloaderIgnoreCachedResponse`
|
||||
|
||||
// This is weak because it is injected by whoever manages this session. If this gets nil-ed out, we won't be able to run
|
||||
// the task associated with this operation
|
||||
|
@ -38,8 +41,9 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
|
|||
|
||||
@property (strong, nonatomic, readwrite, nullable) NSURLSessionTask *dataTask;
|
||||
|
||||
@property (strong, nonatomic, nullable) dispatch_queue_t barrierQueue;
|
||||
@property (strong, nonatomic, nonnull) dispatch_semaphore_t callbacksLock; // a lock to keep the access to `callbackBlocks` thread-safe
|
||||
|
||||
@property (strong, nonatomic, nonnull) dispatch_queue_t coderQueue; // the queue to do image decoding
|
||||
#if SD_UIKIT
|
||||
@property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundTaskId;
|
||||
#endif
|
||||
|
@ -69,7 +73,8 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
|
|||
_finished = NO;
|
||||
_expectedSize = 0;
|
||||
_unownedSession = session;
|
||||
_barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderOperationBarrierQueue", DISPATCH_QUEUE_CONCURRENT);
|
||||
_callbacksLock = dispatch_semaphore_create(1);
|
||||
_coderQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderOperationCoderQueue", DISPATCH_QUEUE_SERIAL);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
@ -79,30 +84,29 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
|
|||
SDCallbacksDictionary *callbacks = [NSMutableDictionary new];
|
||||
if (progressBlock) callbacks[kProgressCallbackKey] = [progressBlock copy];
|
||||
if (completedBlock) callbacks[kCompletedCallbackKey] = [completedBlock copy];
|
||||
dispatch_barrier_async(self.barrierQueue, ^{
|
||||
[self.callbackBlocks addObject:callbacks];
|
||||
});
|
||||
LOCK(self.callbacksLock);
|
||||
[self.callbackBlocks addObject:callbacks];
|
||||
UNLOCK(self.callbacksLock);
|
||||
return callbacks;
|
||||
}
|
||||
|
||||
- (nullable NSArray<id> *)callbacksForKey:(NSString *)key {
|
||||
__block NSMutableArray<id> *callbacks = nil;
|
||||
dispatch_sync(self.barrierQueue, ^{
|
||||
// We need to remove [NSNull null] because there might not always be a progress block for each callback
|
||||
callbacks = [[self.callbackBlocks valueForKey:key] mutableCopy];
|
||||
[callbacks removeObjectIdenticalTo:[NSNull null]];
|
||||
});
|
||||
return [callbacks copy]; // strip mutability here
|
||||
LOCK(self.callbacksLock);
|
||||
NSMutableArray<id> *callbacks = [[self.callbackBlocks valueForKey:key] mutableCopy];
|
||||
UNLOCK(self.callbacksLock);
|
||||
// We need to remove [NSNull null] because there might not always be a progress block for each callback
|
||||
[callbacks removeObjectIdenticalTo:[NSNull null]];
|
||||
return [callbacks copy]; // strip mutability here
|
||||
}
|
||||
|
||||
- (BOOL)cancel:(nullable id)token {
|
||||
__block BOOL shouldCancel = NO;
|
||||
dispatch_barrier_sync(self.barrierQueue, ^{
|
||||
[self.callbackBlocks removeObjectIdenticalTo:token];
|
||||
if (self.callbackBlocks.count == 0) {
|
||||
shouldCancel = YES;
|
||||
}
|
||||
});
|
||||
BOOL shouldCancel = NO;
|
||||
LOCK(self.callbacksLock);
|
||||
[self.callbackBlocks removeObjectIdenticalTo:token];
|
||||
if (self.callbackBlocks.count == 0) {
|
||||
shouldCancel = YES;
|
||||
}
|
||||
UNLOCK(self.callbacksLock);
|
||||
if (shouldCancel) {
|
||||
[self cancel];
|
||||
}
|
||||
|
@ -242,10 +246,9 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
|
|||
}
|
||||
|
||||
- (void)reset {
|
||||
__weak typeof(self) weakSelf = self;
|
||||
dispatch_barrier_async(self.barrierQueue, ^{
|
||||
[weakSelf.callbackBlocks removeAllObjects];
|
||||
});
|
||||
LOCK(self.callbacksLock);
|
||||
[self.callbackBlocks removeAllObjects];
|
||||
UNLOCK(self.callbacksLock);
|
||||
self.dataTask = nil;
|
||||
|
||||
if (self.ownedSession) {
|
||||
|
@ -316,7 +319,7 @@ didReceiveResponse:(NSURLResponse *)response
|
|||
|
||||
if ((self.options & SDWebImageDownloaderProgressiveDownload) && self.expectedSize > 0) {
|
||||
// Get the image data
|
||||
NSData *imageData = [self.imageData copy];
|
||||
__block NSData *imageData = [self.imageData copy];
|
||||
// Get the total bytes downloaded
|
||||
const NSInteger totalSize = imageData.length;
|
||||
// Get the finish status
|
||||
|
@ -333,16 +336,21 @@ didReceiveResponse:(NSURLResponse *)response
|
|||
}
|
||||
}
|
||||
|
||||
UIImage *image = [self.progressiveCoder incrementallyDecodedImageWithData:imageData finished:finished];
|
||||
if (image) {
|
||||
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
|
||||
image = [self scaledImageForKey:key image:image];
|
||||
if (self.shouldDecompressImages) {
|
||||
image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&data options:@{SDWebImageCoderScaleDownLargeImagesKey: @(NO)}];
|
||||
// progressive decode the image in coder queue
|
||||
dispatch_async(self.coderQueue, ^{
|
||||
UIImage *image = [self.progressiveCoder incrementallyDecodedImageWithData:imageData finished:finished];
|
||||
if (image) {
|
||||
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
|
||||
image = [self scaledImageForKey:key image:image];
|
||||
if (self.shouldDecompressImages) {
|
||||
image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&imageData options:@{SDWebImageCoderScaleDownLargeImagesKey: @(NO)}];
|
||||
}
|
||||
|
||||
// We do not keep the progressive decoding image even when `finished`=YES. Because they are for view rendering but not take full function from downloader options. And some coders implementation may not keep consistent between progressive decoding and normal decoding.
|
||||
|
||||
[self callCompletionBlocksWithImage:image imageData:nil error:nil finished:NO];
|
||||
}
|
||||
|
||||
[self callCompletionBlocksWithImage:image imageData:nil error:nil finished:NO];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for (SDWebImageDownloaderProgressBlock progressBlock in [self callbacksForKey:kProgressCallbackKey]) {
|
||||
|
@ -380,14 +388,16 @@ didReceiveResponse:(NSURLResponse *)response
|
|||
});
|
||||
}
|
||||
|
||||
// make sure to call `[self done]` to mark operation as finished
|
||||
if (error) {
|
||||
[self callCompletionBlocksWithError:error];
|
||||
[self done];
|
||||
} else {
|
||||
if ([self callbacksForKey:kCompletedCallbackKey].count > 0) {
|
||||
/**
|
||||
* If you specified to use `NSURLCache`, then the response you get here is what you need.
|
||||
*/
|
||||
NSData *imageData = [self.imageData copy];
|
||||
__block NSData *imageData = [self.imageData copy];
|
||||
if (imageData) {
|
||||
/** if you specified to only use cached data via `SDWebImageDownloaderIgnoreCachedResponse`,
|
||||
* then we should check if the cached data is equal to image data
|
||||
|
@ -395,43 +405,50 @@ didReceiveResponse:(NSURLResponse *)response
|
|||
if (self.options & SDWebImageDownloaderIgnoreCachedResponse && [self.cachedData isEqualToData:imageData]) {
|
||||
// call completion block with nil
|
||||
[self callCompletionBlocksWithImage:nil imageData:nil error:nil finished:YES];
|
||||
[self done];
|
||||
} else {
|
||||
UIImage *image = [[SDWebImageCodersManager sharedInstance] decodedImageWithData:imageData];
|
||||
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
|
||||
image = [self scaledImageForKey:key image:image];
|
||||
|
||||
BOOL shouldDecode = YES;
|
||||
// Do not force decoding animated GIFs and WebPs
|
||||
if (image.images) {
|
||||
shouldDecode = NO;
|
||||
} else {
|
||||
#ifdef SD_WEBP
|
||||
SDImageFormat imageFormat = [NSData sd_imageFormatForImageData:imageData];
|
||||
if (imageFormat == SDImageFormatWebP) {
|
||||
// decode the image in coder queue
|
||||
dispatch_async(self.coderQueue, ^{
|
||||
UIImage *image = [[SDWebImageCodersManager sharedInstance] decodedImageWithData:imageData];
|
||||
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
|
||||
image = [self scaledImageForKey:key image:image];
|
||||
|
||||
BOOL shouldDecode = YES;
|
||||
// Do not force decoding animated GIFs and WebPs
|
||||
if (image.images) {
|
||||
shouldDecode = NO;
|
||||
}
|
||||
} else {
|
||||
#ifdef SD_WEBP
|
||||
SDImageFormat imageFormat = [NSData sd_imageFormatForImageData:imageData];
|
||||
if (imageFormat == SDImageFormatWebP) {
|
||||
shouldDecode = NO;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (shouldDecode) {
|
||||
if (self.shouldDecompressImages) {
|
||||
BOOL shouldScaleDown = self.options & SDWebImageDownloaderScaleDownLargeImages;
|
||||
image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&imageData options:@{SDWebImageCoderScaleDownLargeImagesKey: @(shouldScaleDown)}];
|
||||
}
|
||||
}
|
||||
CGSize imageSize = image.size;
|
||||
if (imageSize.width == 0 || imageSize.height == 0) {
|
||||
[self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}]];
|
||||
} else {
|
||||
[self callCompletionBlocksWithImage:image imageData:imageData error:nil finished:YES];
|
||||
}
|
||||
|
||||
if (shouldDecode) {
|
||||
if (self.shouldDecompressImages) {
|
||||
BOOL shouldScaleDown = self.options & SDWebImageDownloaderScaleDownLargeImages;
|
||||
image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&imageData options:@{SDWebImageCoderScaleDownLargeImagesKey: @(shouldScaleDown)}];
|
||||
}
|
||||
}
|
||||
CGSize imageSize = image.size;
|
||||
if (imageSize.width == 0 || imageSize.height == 0) {
|
||||
[self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}]];
|
||||
} else {
|
||||
[self callCompletionBlocksWithImage:image imageData:imageData error:nil finished:YES];
|
||||
}
|
||||
[self done];
|
||||
});
|
||||
}
|
||||
} else {
|
||||
[self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Image data is nil"}]];
|
||||
[self done];
|
||||
}
|
||||
} else {
|
||||
[self done];
|
||||
}
|
||||
}
|
||||
[self done];
|
||||
}
|
||||
|
||||
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
|
||||
|
|
Loading…
Reference in New Issue