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

Conversation

ernestmama
Copy link
Contributor

add more delegate methods for monitoring network image node progress so we can better keep track of optimization opportunity

Copy link
Member

@nguyenhuy nguyenhuy left a 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!

@@ -335,6 +343,10 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously
{
[super displayWillStartAsynchronously:asynchronously];

if (_delegateFlags.delegateWillStartDisplayAsynchronously) {
Copy link
Contributor

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

Copy link
Contributor Author

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];
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.

Copy link
Member

@nguyenhuy nguyenhuy left a 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];
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 (self->_delegateFlags.delegateDidLoadImageFromCache) {
Copy link
Member

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.

as_log_verbose(ASImageLoadingLog(), "Decached image for %@ img: %@ url: %@", self, [imageContainer asdk_image], URL);
finished(imageContainer, nil, nil, ASNetworkImageSourceAsynchronousCache, nil);
}
};

if (self->_delegateFlags.delegateWillLoadImageFromCache) {
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

[_cache cachedImageWithURL:URL
callbackQueue:[self callbackQueue]
completion:completion];
} else {
if (self->_delegateFlags.delegateWillLoadImageFromNetwork) {
Copy link
Member

Choose a reason for hiding this comment

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

And here.

id<ASNetworkImageNodeDelegate> delegate;
{
delegate = _delegate;
}
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.

Just use the cached delegate above.

Copy link
Member

@nguyenhuy nguyenhuy left a comment

Choose a reason for hiding this comment

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

1 last nit.

id<ASNetworkImageNodeDelegate> delegate;
BOOL notifyDelegate;
{
ASLockedSelf();
Copy link
Member

Choose a reason for hiding this comment

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

Should be ASLockScopeSelf.

@nguyenhuy
Copy link
Member

@maicki Can you take another look? Thanks!

@ernestmama
Copy link
Contributor Author

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;
{
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.

@maicki
Copy link
Contributor

maicki commented Nov 27, 2018

@ernestmama @nguyenhuy Added one comment. This should not be a blocker though as in fact we have this problem in other places too.

@nguyenhuy nguyenhuy merged commit 4d1ffcc into TextureGroup:master Nov 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants