-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from all commits
2e3aa22
3807af4
660b4bf
105ed20
552f94b
c21b5e2
7cca4a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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:)]; | ||
} | ||
|
||
|
@@ -335,6 +343,17 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously | |
{ | ||
[super displayWillStartAsynchronously:asynchronously]; | ||
|
||
id<ASNetworkImageNodeDelegate> delegate; | ||
BOOL notifyDelegate; | ||
{ | ||
ASLockScopeSelf(); | ||
notifyDelegate = _delegateFlags.delegateWillStartDisplayAsynchronously; | ||
delegate = _delegate; | ||
} | ||
if (notifyDelegate) { | ||
[delegate imageNodeWillStartDisplayAsynchronously:self]; | ||
} | ||
|
||
if (asynchronously == NO && _cacheFlags.cacheSupportsSynchronousFetch) { | ||
ASLockScopeSelf(); | ||
|
||
|
@@ -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]; | ||
} | ||
} | ||
} | ||
|
@@ -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; | ||
|
@@ -773,18 +795,31 @@ - (void)_lazilyLoadImageIfNecessary | |
} | ||
|
||
if ([imageContainer asdk_image] == nil && self->_downloader != nil) { | ||
if (delegateWillLoadImageFromNetwork) { | ||
[delegate imageNodeWillLoadImageFromNetwork:self]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
[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); | ||
}]; | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.