Skip to content

Commit

Permalink
iOS: Fix image instrumentation lifecycle on image cancel
Browse files Browse the repository at this point in the history
Summary: Internal loggers were not deallocated when images were canceled on RCTImageView

Reviewed By: fkgozali

Differential Revision: D21380284

fbshipit-source-id: 00440cf49708ec03ecd7d9268001aa458ccbf923
  • Loading branch information
p-sun authored and facebook-github-bot committed May 15, 2020
1 parent 452cb9a commit 6cba4d2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
16 changes: 14 additions & 2 deletions Libraries/Image/RCTImageLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req
}
}
});

return [[RCTImageURLLoaderRequest alloc] initWithRequestId:requestId imageURL:request.URL cancellationBlock:^{
BOOL alreadyCancelled = atomic_fetch_or(cancelled.get(), 1);
if (alreadyCancelled) {
Expand Down Expand Up @@ -801,12 +801,24 @@ - (void)trackURLImageVisibilityForRequest:(RCTImageURLLoaderRequest *)loaderRequ
}
}

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

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

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

id<RCTImageURLLoader> loadHandler = [self imageURLLoaderForURL:loaderRequest.imageURL];
if ([loadHandler respondsToSelector:@selector(trackURLImageDidDestroy:)]) {
[(id<RCTImageURLLoaderWithAttribution>)loadHandler trackURLImageDidDestroy:loaderRequest];
Expand Down
5 changes: 5 additions & 0 deletions Libraries/Image/RCTImageLoaderWithAttributionProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ RCT_EXTERN void RCTEnableImageLoadingPerfInstrumentation(BOOL enabled);
*/
- (void)trackURLImageVisibilityForRequest:(RCTImageURLLoaderRequest *)loaderRequest imageView:(UIView *)imageView;

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

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

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

/**
* Image instrumentation - notify that the native image view was destroyed.
*/
Expand Down
5 changes: 2 additions & 3 deletions Libraries/Image/RCTImageView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,8 @@ - (void)setResizeMode:(RCTResizeMode)resizeMode

- (void)cancelImageLoad
{
if (_loaderRequest.cancellationBlock) {
_loaderRequest.cancellationBlock();
}
[_loaderRequest cancel];
[_imageLoader trackURLImageRequestDidCancel:_loaderRequest];

_loaderRequest = nil;
_pendingImageSource = nil;
Expand Down

0 comments on commit 6cba4d2

Please sign in to comment.