Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more delegate methods for monitoring network image node progress #1247

Merged
merged 7 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
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
37 changes: 37 additions & 0 deletions Source/ASNetworkImageNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,42 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)imageNodeDidStartFetchingData:(ASNetworkImageNode *)imageNode;

/**
* Notification that the image node will load image from cache
*
* @param imageNode The sender.
*
* @discussion Called on the main thread.
*/
- (void)imageNodeWillLoadImageFromCache:(ASNetworkImageNode *)imageNode;

/**
* Notification that the image node finished loading image from cache
*
* @param imageNode The sender.
*
* @discussion Called on the main thread.
*/
- (void)imageNodeDidLoadImageFromCache:(ASNetworkImageNode *)imageNode;

/**
* Notification that the image node will load image from network
*
* @param imageNode The sender.
*
* @discussion Called on the main thread.
*/
- (void)imageNodeWillLoadImageFromNetwork:(ASNetworkImageNode *)imageNode;

/**
* Notification that the image node will start display
*
* @param imageNode The sender.
*
* @discussion Called on the main thread.
*/
- (void)imageNodeWillStartDisplayAsynchronously:(ASNetworkImageNode *)imageNode;

/**
* Notification that the image node finished downloading an image, with additional info.
* If implemented, this method will be called instead of `imageNode:didLoadImage:`.
Expand Down Expand Up @@ -195,6 +231,7 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)imageNodeDidFinishDecoding:(ASNetworkImageNode *)imageNode;


@end

NS_ASSUME_NONNULL_END
39 changes: 37 additions & 2 deletions Source/ASNetworkImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ @interface ASNetworkImageNode ()
BOOL _shouldRenderProgressImages;

struct {
unsigned int delegateWillStartDisplayAsynchronously:1;
unsigned int delegateWillLoadImageFromCache:1;
unsigned int delegateWillLoadImageFromNetwork:1;
unsigned int delegateDidStartFetchingData:1;
unsigned int delegateDidFailWithError:1;
unsigned int delegateDidFinishDecoding:1;
unsigned int delegateDidLoadImage:1;
unsigned int delegateDidLoadImageFromCache:1;
unsigned int delegateDidLoadImageWithInfo:1;
} _delegateFlags;

Expand Down Expand Up @@ -297,10 +301,14 @@ - (void)setDelegate:(id<ASNetworkImageNodeDelegate>)delegate
ASLockScopeSelf();
_delegate = delegate;

_delegateFlags.delegateWillStartDisplayAsynchronously = [delegate respondsToSelector:@selector(imageNodeWillStartDisplayAsynchronously:)];
_delegateFlags.delegateWillLoadImageFromCache = [delegate respondsToSelector:@selector(imageNodeWillLoadImageFromCache:)];
_delegateFlags.delegateWillLoadImageFromNetwork = [delegate respondsToSelector:@selector(imageNodeWillLoadImageFromNetwork:)];
_delegateFlags.delegateDidStartFetchingData = [delegate respondsToSelector:@selector(imageNodeDidStartFetchingData:)];
_delegateFlags.delegateDidFailWithError = [delegate respondsToSelector:@selector(imageNode:didFailWithError:)];
_delegateFlags.delegateDidFinishDecoding = [delegate respondsToSelector:@selector(imageNodeDidFinishDecoding:)];
_delegateFlags.delegateDidLoadImage = [delegate respondsToSelector:@selector(imageNode:didLoadImage:)];
_delegateFlags.delegateDidLoadImageFromCache = [delegate respondsToSelector:@selector(imageNodeDidLoadImageFromCache:)];
_delegateFlags.delegateDidLoadImageWithInfo = [delegate respondsToSelector:@selector(imageNode:didLoadImage:info:)];
}

Expand Down Expand Up @@ -335,6 +343,17 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously
{
[super displayWillStartAsynchronously:asynchronously];

id<ASNetworkImageNodeDelegate> delegate;
BOOL notifyDelegate;
{
Copy link
Contributor

Choose a reason for hiding this comment

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

It's fine to grab the delegate in here with the lock held. One problem that will occur, what is not new and in fact we have it in almost all of our unlocked delegate calls, if the delegate needs more information from the node the information could already be out of date. Especially if you use this methods for logging this could introduce inconsistencies.

This is not a new problem though and we think about different solutions to find some general solution for this though.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, it's not a new problem, and I'd say delegates should rely on the data being passed via the delegate method. Not an ideal solution, but it's a fine trade-off.

I'm gonna land this PR now. Thanks for reviewing.

ASLockScopeSelf();
notifyDelegate = _delegateFlags.delegateWillStartDisplayAsynchronously;
delegate = _delegate;
}
if (notifyDelegate) {
[delegate imageNodeWillStartDisplayAsynchronously:self];
}

if (asynchronously == NO && _cacheFlags.cacheSupportsSynchronousFetch) {
ASLockScopeSelf();

Expand All @@ -350,10 +369,10 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously
if (_delegateFlags.delegateDidLoadImageWithInfo) {
ASUnlockScope(self);
let info = [[ASNetworkImageLoadInfo alloc] initWithURL:url sourceType:ASNetworkImageSourceSynchronousCache downloadIdentifier:nil userInfo:nil];
[_delegate imageNode:self didLoadImage:result info:info];
[delegate imageNode:self didLoadImage:result info:info];
} else if (_delegateFlags.delegateDidLoadImage) {
ASUnlockScope(self);
[_delegate imageNode:self didLoadImage:result];
[delegate imageNode:self didLoadImage:result];
}
}
}
Expand Down Expand Up @@ -616,6 +635,9 @@ - (void)_lazilyLoadImageIfNecessary
[self lock];
__weak id<ASNetworkImageNodeDelegate> delegate = _delegate;
BOOL delegateDidStartFetchingData = _delegateFlags.delegateDidStartFetchingData;
BOOL delegateWillLoadImageFromCache = _delegateFlags.delegateWillLoadImageFromCache;
BOOL delegateWillLoadImageFromNetwork = _delegateFlags.delegateWillLoadImageFromNetwork;
BOOL delegateDidLoadImageFromCache = _delegateFlags.delegateDidLoadImageFromCache;
BOOL isImageLoaded = _imageLoaded;
NSURL *URL = _URL;
id currentDownloadIdentifier = _downloadIdentifier;
Expand Down Expand Up @@ -773,18 +795,31 @@ - (void)_lazilyLoadImageIfNecessary
}

if ([imageContainer asdk_image] == nil && self->_downloader != nil) {
if (delegateWillLoadImageFromNetwork) {
[delegate imageNodeWillLoadImageFromNetwork:self];
Copy link
Contributor

Choose a reason for hiding this comment

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

All of this methods are called with the lock held. This can cause deadlocks. We should at least document this behavior if we cannot get around this.

Copy link
Contributor Author

@ernestmama ernestmama Nov 26, 2018

Choose a reason for hiding this comment

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

I don't think these are locked. I tried putting a ASUnlockScope which result in an assertion failure. @nguyenhuy

Copy link
Member

@nguyenhuy nguyenhuy Nov 27, 2018

Choose a reason for hiding this comment

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

This is inside a completion block and the lock is no longer held by the time it's executed. That's why we grabbed the lock before checking the cache sentinel a few line above. So we don't need to unlock here, but we need to grab the lock before accessing _delegateFlags. We can do so at the beginning of this method, similar to how we check the _delegateFlags.delegateDidStartFetchingData flag.

}
[self _downloadImageWithCompletion:^(id<ASImageContainerProtocol> imageContainer, NSError *error, id downloadIdentifier, id userInfo) {
finished(imageContainer, error, downloadIdentifier, ASNetworkImageSourceDownload, userInfo);
}];
} else {
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);
}
};

if (delegateWillLoadImageFromCache) {
[delegate imageNodeWillLoadImageFromCache:self];
}
[_cache cachedImageWithURL:URL
callbackQueue:[self callbackQueue]
completion:completion];
} else {
if (delegateWillLoadImageFromNetwork) {
[delegate imageNodeWillLoadImageFromNetwork:self];
}
[self _downloadImageWithCompletion:^(id<ASImageContainerProtocol> imageContainer, NSError *error, id downloadIdentifier, id userInfo) {
finished(imageContainer, error, downloadIdentifier, ASNetworkImageSourceDownload, userInfo);
}];
Expand Down