Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

[ASNetworkImageNode] Dispatch deallocation of UIImage in clearFetchedData: to background thread #1303

Merged
merged 1 commit into from
Mar 2, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions AsyncDisplayKit/ASNetworkImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#import "ASPINRemoteImageDownloader.h"
#endif

static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0};

@interface ASNetworkImageNode ()
{
ASDN::RecursiveMutex _lock;
Expand Down Expand Up @@ -228,8 +230,7 @@ - (void)clearFetchedData
ASDN::MutexLocker l(_lock);

[self _cancelImageDownload];
self.image = _defaultImage;
_imageLoaded = NO;
[self _clearImage];
if (_cacheSupportsClearing) {
[_cache clearFetchedImageFromCacheWithURL:_URL];
}
Expand All @@ -248,6 +249,25 @@ - (void)fetchData

#pragma mark - Private methods -- only call with lock.

- (void)_clearImage
{
// Destruction of bigger images on the main thread can be expensive
// and can take some time, so we dispatch onto a bg queue to
// actually dealloc.
UIImage *image = self.image;
CGSize imageSize = image.size;
BOOL shouldReleaseImageOnBackgroundThread = imageSize.width > kMinReleaseImageOnBackgroundSize.width ||
imageSize.height > kMinReleaseImageOnBackgroundSize.height;
if (shouldReleaseImageOnBackgroundThread) {
__block UIImage *blockImage = image;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically we should not need __block here, as we don't need to write to the variable but just have it go out of scope. simply referencing the image with (__unused) or something should be enough - but this is fine too! :)

ASPerformBlockOnBackgroundThread(^{
blockImage = nil;
});
}
self.image = _defaultImage;
_imageLoaded = NO;
}

- (void)_cancelImageDownload
{
if (!_downloadIdentifier) {
Expand Down