Skip to content

Commit

Permalink
Fix for images retrieved from the memory cache being reported a… (#1722)
Browse files Browse the repository at this point in the history
* Fix for incorrectly reporting images retrieved from the memory cache as disk (i.e. asynchronous) cache hits.
  • Loading branch information
darrengyles authored and jparise committed Nov 7, 2019
1 parent 278ea43 commit 744bb3e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Source/ASMultiplexImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ - (void)_fetchImageWithIdentifierFromCache:(id)imageIdentifier URL:(NSURL *)imag
ASDisplayNodeAssertNotNil(completionBlock, @"completionBlock is required");

if (_cache) {
[_cache cachedImageWithURL:imageURL callbackQueue:dispatch_get_main_queue() completion:^(id <ASImageContainerProtocol> imageContainer) {
[_cache cachedImageWithURL:imageURL callbackQueue:dispatch_get_main_queue() completion:^(id <ASImageContainerProtocol> imageContainer, __unused ASImageCacheType cacheType ) {
completionBlock([imageContainer asdk_image]);
}];
}
Expand Down
6 changes: 3 additions & 3 deletions Source/ASNetworkImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ - (void)_lazilyLoadImageIfNecessary

as_log_verbose(ASImageLoadingLog(), "Decaching image for %@ url: %@", self, URL);

ASImageCacherCompletion completion = ^(id <ASImageContainerProtocol> imageContainer) {
ASImageCacherCompletion completion = ^(id <ASImageContainerProtocol> imageContainer, ASImageCacheType cacheType) {
// If the cache sentinel changed, that means this request was cancelled.
if (ASLockedSelf(self->_cacheSentinel != cacheSentinel)) {
return;
Expand All @@ -887,8 +887,8 @@ - (void)_lazilyLoadImageIfNecessary
if (delegateDidLoadImageFromCache) {
[delegate imageNodeDidLoadImageFromCache:self];
}
as_log_verbose(ASImageLoadingLog(), "Decached image for %@ img: %@ url: %@", self, [imageContainer asdk_image], URL);
finished(imageContainer, nil, nil, ASNetworkImageSourceAsynchronousCache, nil);
as_log_verbose(ASImageLoadingLog(), "Decached image for %@ img: %@ url: %@ cacheType: %@", self, [imageContainer asdk_image], URL, cacheType);
finished(imageContainer, nil, nil, cacheType == ASImageCacheTypeSynchronous ? ASNetworkImageSourceSynchronousCache : ASNetworkImageSourceAsynchronousCache, nil);
}
};

Expand Down
7 changes: 6 additions & 1 deletion Source/Details/ASImageProtocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ NS_ASSUME_NONNULL_BEGIN

@end

typedef void(^ASImageCacherCompletion)(id <ASImageContainerProtocol> _Nullable imageFromCache);
typedef NS_ENUM(NSInteger, ASImageCacheType) {
ASImageCacheTypeAsynchronous = 0,
ASImageCacheTypeSynchronous,
};

typedef void(^ASImageCacherCompletion)(id <ASImageContainerProtocol> _Nullable imageFromCache, ASImageCacheType cacheType);

@protocol ASImageCacheProtocol <NSObject>

Expand Down
7 changes: 4 additions & 3 deletions Source/Details/ASPINRemoteImageDownloader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,15 @@ - (void)cachedImageWithURL:(NSURL *)URL
{
[[self sharedPINRemoteImageManager] imageFromCacheWithURL:URL processorKey:nil options:PINRemoteImageManagerDownloadOptionsSkipDecode completion:^(PINRemoteImageManagerResult * _Nonnull result) {
[ASPINRemoteImageDownloader _performWithCallbackQueue:callbackQueue work:^{
ASImageCacheType cacheType = (result.resultType == PINRemoteImageResultTypeMemoryCache ? ASImageCacheTypeSynchronous : ASImageCacheTypeAsynchronous);
#if PIN_ANIMATED_AVAILABLE
if (result.alternativeRepresentation) {
completion(result.alternativeRepresentation);
completion(result.alternativeRepresentation, cacheType);
} else {
completion(result.image);
completion(result.image, cacheType);
}
#else
completion(result.image);
completion(result.image, cacheType);
#endif
}];
}];
Expand Down
4 changes: 2 additions & 2 deletions Tests/ASMultiplexImageNodeTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ - (void)testDataSourceURLMethod
OCMExpect([mockCache cachedImageWithURL:[self _testImageURL] callbackQueue:OCMOCK_ANY completion:[OCMArg isNotNil]])
.andDo(^(NSInvocation *inv) {
ASImageCacherCompletion completionBlock = [inv as_argumentAtIndexAsObject:4];
completionBlock([self _testImage]);
completionBlock([self _testImage], ASImageCacheTypeAsynchronous);
});

imageNode.imageIdentifiers = @[imageIdentifier];
Expand Down Expand Up @@ -217,7 +217,7 @@ - (void)testUncachedDownload
OCMExpect([mockCache cachedImageWithURL:[self _testImageURL] callbackQueue:OCMOCK_ANY completion:[OCMArg isNotNil]])
.andDo(^(NSInvocation *inv){
ASImageCacherCompletion completion = [inv as_argumentAtIndexAsObject:4];
completion(nil);
completion(nil, ASImageCacheTypeAsynchronous);
});

// Mock a 50%-progress URL download.
Expand Down
2 changes: 1 addition & 1 deletion Tests/ASNetworkImageNodeTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ @implementation ASTestImageCache

- (void)cachedImageWithURL:(NSURL *)URL callbackQueue:(dispatch_queue_t)callbackQueue completion:(ASImageCacherCompletion)completion
{
completion(nil);
completion(nil, ASImageCacheTypeAsynchronous);
}

@end
Expand Down

0 comments on commit 744bb3e

Please sign in to comment.