[AS*ImageNode] dispatch to the background before scheduling image download request (perf).

This commit is contained in:
Scott Goodson
2016-02-20 13:01:03 -08:00
parent cd6ca2885e
commit ca8357a364
2 changed files with 66 additions and 59 deletions

View File

@@ -21,6 +21,7 @@
#import "ASLog.h" #import "ASLog.h"
#import "ASPhotosFrameworkImageRequest.h" #import "ASPhotosFrameworkImageRequest.h"
#import "ASEqualityHelpers.h" #import "ASEqualityHelpers.h"
#import "ASInternalHelpers.h"
#if !AS_IOS8_SDK_OR_LATER #if !AS_IOS8_SDK_OR_LATER
#error ASMultiplexImageNode can be used on iOS 7, but must be linked against the iOS 8 SDK. #error ASMultiplexImageNode can be used on iOS 7, but must be linked against the iOS 8 SDK.
@@ -751,46 +752,48 @@ typedef void(^ASMultiplexImageLoadCompletionBlock)(UIImage *image, id imageIdent
} }
// Download! // Download!
if (_downloaderSupportsNewProtocol) { ASPerformBlockOnBackgroundThread(^{
[self _setDownloadIdentifier:[_downloader downloadImageWithURL:imageURL if (_downloaderSupportsNewProtocol) {
callbackQueue:dispatch_get_main_queue() [self _setDownloadIdentifier:[_downloader downloadImageWithURL:imageURL
downloadProgress:downloadProgressBlock callbackQueue:dispatch_get_main_queue()
completion:^(UIImage *downloadedImage, NSError *error, id downloadIdentifier) { downloadProgress:downloadProgressBlock
// We dereference iVars directly, so we can't have weakSelf going nil on us. completion:^(UIImage *downloadedImage, NSError *error, id downloadIdentifier) {
__typeof__(self) strongSelf = weakSelf; // We dereference iVars directly, so we can't have weakSelf going nil on us.
if (!strongSelf) __typeof__(self) strongSelf = weakSelf;
return; if (!strongSelf)
return;
ASDN::MutexLocker l(_downloadIdentifierLock);
//Getting a result back for a different download identifier, download must not have been successfully canceled ASDN::MutexLocker l(_downloadIdentifierLock);
if (ASObjectIsEqual(_downloadIdentifier, downloadIdentifier) == NO && downloadIdentifier != nil) { //Getting a result back for a different download identifier, download must not have been successfully canceled
return; if (ASObjectIsEqual(_downloadIdentifier, downloadIdentifier) == NO && downloadIdentifier != nil) {
} return;
}
completionBlock(downloadedImage, error);
completionBlock(downloadedImage, error);
// Delegateify.
if (strongSelf->_delegateFlags.downloadFinish) // Delegateify.
[strongSelf->_delegate multiplexImageNode:weakSelf didFinishDownloadingImageWithIdentifier:imageIdentifier error:error]; if (strongSelf->_delegateFlags.downloadFinish)
}]]; [strongSelf->_delegate multiplexImageNode:weakSelf didFinishDownloadingImageWithIdentifier:imageIdentifier error:error];
} else { }]];
[self _setDownloadIdentifier:[_downloader downloadImageWithURL:imageURL } else {
callbackQueue:dispatch_get_main_queue() [self _setDownloadIdentifier:[_downloader downloadImageWithURL:imageURL
downloadProgressBlock:downloadProgressBlock callbackQueue:dispatch_get_main_queue()
completion:^(CGImageRef coreGraphicsImage, NSError *error) { downloadProgressBlock:downloadProgressBlock
// We dereference iVars directly, so we can't have weakSelf going nil on us. completion:^(CGImageRef coreGraphicsImage, NSError *error) {
__typeof__(self) strongSelf = weakSelf; // We dereference iVars directly, so we can't have weakSelf going nil on us.
if (!strongSelf) __typeof__(self) strongSelf = weakSelf;
return; if (!strongSelf)
return;
UIImage *downloadedImage = (coreGraphicsImage ? [UIImage imageWithCGImage:coreGraphicsImage] : nil);
completionBlock(downloadedImage, error); UIImage *downloadedImage = (coreGraphicsImage ? [UIImage imageWithCGImage:coreGraphicsImage] : nil);
completionBlock(downloadedImage, error);
// Delegateify.
if (strongSelf->_delegateFlags.downloadFinish) // Delegateify.
[strongSelf->_delegate multiplexImageNode:weakSelf didFinishDownloadingImageWithIdentifier:imageIdentifier error:error]; if (strongSelf->_delegateFlags.downloadFinish)
}]]; [strongSelf->_delegate multiplexImageNode:weakSelf didFinishDownloadingImageWithIdentifier:imageIdentifier error:error];
} }]];
}
});
} }
#pragma mark - #pragma mark -

View File

@@ -13,6 +13,7 @@
#import "ASDisplayNode+FrameworkPrivate.h" #import "ASDisplayNode+FrameworkPrivate.h"
#import "ASEqualityHelpers.h" #import "ASEqualityHelpers.h"
#import "ASThread.h" #import "ASThread.h"
#import "ASInternalHelpers.h"
#if PIN_REMOTE_IMAGE #if PIN_REMOTE_IMAGE
#import "ASPINRemoteImageDownloader.h" #import "ASPINRemoteImageDownloader.h"
@@ -263,25 +264,28 @@
- (void)_downloadImageWithCompletion:(void (^)(UIImage *image, NSError*, id downloadIdentifier))finished - (void)_downloadImageWithCompletion:(void (^)(UIImage *image, NSError*, id downloadIdentifier))finished
{ {
if (_downloaderSupportsNewProtocol) { ASPerformBlockOnBackgroundThread(^{
_downloadIdentifier = [_downloader downloadImageWithURL:_URL ASDN::MutexLocker l(_lock);
callbackQueue:dispatch_get_main_queue() if (_downloaderSupportsNewProtocol) {
downloadProgress:NULL _downloadIdentifier = [_downloader downloadImageWithURL:_URL
completion:^(UIImage * _Nullable image, NSError * _Nullable error, id _Nullable downloadIdentifier) { callbackQueue:dispatch_get_main_queue()
if (finished != NULL) { downloadProgress:NULL
finished(image, error, downloadIdentifier); completion:^(UIImage * _Nullable image, NSError * _Nullable error, id _Nullable downloadIdentifier) {
} if (finished != NULL) {
}]; finished(image, error, downloadIdentifier);
} else { }
_downloadIdentifier = [_downloader downloadImageWithURL:_URL }];
callbackQueue:dispatch_get_main_queue() } else {
downloadProgressBlock:NULL _downloadIdentifier = [_downloader downloadImageWithURL:_URL
completion:^(CGImageRef responseImage, NSError *error) { callbackQueue:dispatch_get_main_queue()
if (finished != NULL) { downloadProgressBlock:NULL
finished([UIImage imageWithCGImage:responseImage], error, nil); completion:^(CGImageRef responseImage, NSError *error) {
} if (finished != NULL) {
}]; finished([UIImage imageWithCGImage:responseImage], error, nil);
} }
}];
}
});
} }
- (void)_lazilyLoadImageIfNecessary - (void)_lazilyLoadImageIfNecessary