-
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
add more delegate methods for monitoring network image node progress #1247
Conversation
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.
Straight-forward additions, LGTM!
Source/ASNetworkImageNode.mm
Outdated
@@ -335,6 +343,10 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously | |||
{ | |||
[super displayWillStartAsynchronously:asynchronously]; | |||
|
|||
if (_delegateFlags.delegateWillStartDisplayAsynchronously) { |
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.
We don't grab a lock here to access the _delegateFlags
and the _delegate
here
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.
will update
@@ -773,18 +785,31 @@ - (void)_lazilyLoadImageIfNecessary | |||
} | |||
|
|||
if ([imageContainer asdk_image] == nil && self->_downloader != nil) { | |||
if (self->_delegateFlags.delegateWillLoadImageFromNetwork) { | |||
[delegate imageNodeWillLoadImageFromNetwork:self]; |
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.
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 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
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.
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.
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.
Good call on locking before accessing these properties, @maicki. Reviewed this diff again (more carefully this time).
@@ -773,18 +785,31 @@ - (void)_lazilyLoadImageIfNecessary | |||
} | |||
|
|||
if ([imageContainer asdk_image] == nil && self->_downloader != nil) { | |||
if (self->_delegateFlags.delegateWillLoadImageFromNetwork) { | |||
[delegate imageNodeWillLoadImageFromNetwork:self]; |
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.
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.
Source/ASNetworkImageNode.mm
Outdated
[self _downloadImageWithCompletion:^(id<ASImageContainerProtocol> imageContainer, NSError *error, id downloadIdentifier, id userInfo) { | ||
finished(imageContainer, error, downloadIdentifier, ASNetworkImageSourceDownload, userInfo); | ||
}]; | ||
} else { | ||
if (self->_delegateFlags.delegateDidLoadImageFromCache) { |
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.
Grab the flag at the beginning of this method and use it here.
Source/ASNetworkImageNode.mm
Outdated
as_log_verbose(ASImageLoadingLog(), "Decached image for %@ img: %@ url: %@", self, [imageContainer asdk_image], URL); | ||
finished(imageContainer, nil, nil, ASNetworkImageSourceAsynchronousCache, nil); | ||
} | ||
}; | ||
|
||
if (self->_delegateFlags.delegateWillLoadImageFromCache) { |
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.
Same here.
Source/ASNetworkImageNode.mm
Outdated
[_cache cachedImageWithURL:URL | ||
callbackQueue:[self callbackQueue] | ||
completion:completion]; | ||
} else { | ||
if (self->_delegateFlags.delegateWillLoadImageFromNetwork) { |
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.
And here.
Source/ASNetworkImageNode.mm
Outdated
id<ASNetworkImageNodeDelegate> delegate; | ||
{ | ||
delegate = _delegate; | ||
} |
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.
Just use the cached delegate
above.
…ure into networkImageNodeDelegate
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.
1 last nit.
Source/ASNetworkImageNode.mm
Outdated
id<ASNetworkImageNodeDelegate> delegate; | ||
BOOL notifyDelegate; | ||
{ | ||
ASLockedSelf(); |
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.
Should be ASLockScopeSelf
.
@maicki Can you take another look? Thanks! |
Thanks @maicki and @nguyenhuy for helping on the Lock issue! |
@@ -335,6 +343,17 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously | |||
{ | |||
[super displayWillStartAsynchronously:asynchronously]; | |||
|
|||
id<ASNetworkImageNodeDelegate> delegate; | |||
BOOL notifyDelegate; | |||
{ |
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.
@ernestmama @nguyenhuy Added one comment. This should not be a blocker though as in fact we have this problem in other places too. |
add more delegate methods for monitoring network image node progress so we can better keep track of optimization opportunity