Skip to content

Commit

Permalink
iOS: Fix logging lifecycle when image is scrolled out and immediately…
Browse files Browse the repository at this point in the history
… back in

Reviewed By: fkgozali

Differential Revision: D21619910

fbshipit-source-id: b94073afaacad45e12d22d593184cea97612fa26
  • Loading branch information
p-sun authored and facebook-github-bot committed May 18, 2020
1 parent 7929f67 commit 1f95c9b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
6 changes: 3 additions & 3 deletions Libraries/Image/RCTImageLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -801,15 +801,15 @@ - (void)trackURLImageVisibilityForRequest:(RCTImageURLLoaderRequest *)loaderRequ
}
}

- (void)trackURLImageRequestDidCancel:(RCTImageURLLoaderRequest *)loaderRequest
- (void)trackURLImageRequestDidDestroy:(RCTImageURLLoaderRequest *)loaderRequest
{
if (!loaderRequest) {
return;
}

id<RCTImageURLLoader> loadHandler = [self imageURLLoaderForURL:loaderRequest.imageURL];
if ([loadHandler respondsToSelector:@selector(trackURLImageRequestDidCancel:)]) {
[(id<RCTImageURLLoaderWithAttribution>)loadHandler trackURLImageRequestDidCancel:loaderRequest];
if ([loadHandler respondsToSelector:@selector(trackURLImageRequestDidDestroy:)]) {
[(id<RCTImageURLLoaderWithAttribution>)loadHandler trackURLImageRequestDidDestroy:loaderRequest];
}
}

Expand Down
2 changes: 1 addition & 1 deletion Libraries/Image/RCTImageLoaderWithAttributionProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ RCT_EXTERN void RCTEnableImageLoadingPerfInstrumentation(BOOL enabled);
/**
* Image instrumentation - notify that the request was cancelled.
*/
- (void)trackURLImageRequestDidCancel:(RCTImageURLLoaderRequest *)loaderRequest;
- (void)trackURLImageRequestDidDestroy:(RCTImageURLLoaderRequest *)loaderRequest;

/**
* Image instrumentation - notify that the native image view was destroyed.
Expand Down
4 changes: 2 additions & 2 deletions Libraries/Image/RCTImageURLLoaderWithAttribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ struct ImageURLLoaderAttribution {
- (void)trackURLImageVisibilityForRequest:(RCTImageURLLoaderRequest *)loaderRequest imageView:(UIView *)imageView;

/**
* Image instrumentation - notify that the request was cancelled.
* Image instrumentation - notify that the request was destroyed.
*/
- (void)trackURLImageRequestDidCancel:(RCTImageURLLoaderRequest *)loaderRequest;
- (void)trackURLImageRequestDidDestroy:(RCTImageURLLoaderRequest *)loaderRequest;

/**
* Image instrumentation - notify that the native image view was destroyed.
Expand Down
21 changes: 11 additions & 10 deletions Libraries/Image/RCTImageView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ @implementation RCTImageView
BOOL _needsReload;

RCTUIImageViewAnimated *_imageView;

RCTImageURLLoaderRequest *_loaderRequest;
}

Expand Down Expand Up @@ -219,23 +219,23 @@ - (void)setResizeMode:(RCTResizeMode)resizeMode
- (void)cancelImageLoad
{
[_loaderRequest cancel];
[_imageLoader trackURLImageRequestDidCancel:_loaderRequest];

_loaderRequest = nil;
_pendingImageSource = nil;
}

- (void)clearImage
- (void)cancelAndClearImageLoad
{
[self cancelImageLoad];
self.image = nil;
_imageSource = nil;

[_imageLoader trackURLImageRequestDidDestroy:_loaderRequest];
_loaderRequest = nil;
}

- (void)clearImageIfDetached
{
if (!self.window) {
[self clearImage];
[self cancelAndClearImageLoad];
self.image = nil;
_imageSource = nil;
}
}

Expand Down Expand Up @@ -291,7 +291,7 @@ - (BOOL)shouldChangeImageSource

- (void)reloadImage
{
[self cancelImageLoad];
[self cancelAndClearImageLoad];
_needsReload = NO;

RCTImageSource *source = [self imageSourceForSize:self.frame.size];
Expand Down Expand Up @@ -347,7 +347,7 @@ - (void)reloadImage
completionBlock:completionHandler];
_loaderRequest = loaderRequest;
} else {
[self clearImage];
[self cancelAndClearImageLoad];
}
}

Expand Down Expand Up @@ -467,6 +467,7 @@ - (void)didMoveToWindow
// prioritise image requests that are actually on-screen, this removes
// requests that have gotten "stuck" from the queue, unblocking other images
// from loading.
// Do not clear _loaderRequest because this component can be visible again without changing image source
[self cancelImageLoad];
} else if ([self shouldChangeImageSource]) {
[self reloadImage];
Expand Down

0 comments on commit 1f95c9b

Please sign in to comment.