Skip to content

Commit

Permalink
Cancel network requests from the correct queue
Browse files Browse the repository at this point in the history
Summary: Fix suggested by sooth-sayer (facebook#10280)

Reviewed By: mmmulani

Differential Revision: D4001618

fbshipit-source-id: cc28d19d02a29b62d2bdbddcd30f94b1c1bcfd76
(cherry picked from commit 64dd140)
  • Loading branch information
Eric Patey committed Oct 13, 2016
1 parent 0552f5c commit 80c1f09
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
17 changes: 13 additions & 4 deletions Libraries/Image/RCTImageLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ - (RCTImageLoaderCancellationBlock)_loadURLRequest:(NSURLRequest *)request

// Download image
__weak __typeof(self) weakSelf = self;
RCTNetworkTask *task = [networking networkTaskWithRequest:request completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) {
__block RCTNetworkTask *task =
[networking networkTaskWithRequest:request
completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) {
__typeof(self) strongSelf = weakSelf;
if (!strongSelf) {
return;
Expand Down Expand Up @@ -491,8 +493,15 @@ - (RCTImageLoaderCancellationBlock)_loadURLRequest:(NSURLRequest *)request
}

return ^{
[task cancel];
[weakSelf dequeueTasks];
__typeof(self) strongSelf = weakSelf;
if (!strongSelf || !task) {
return;
}
dispatch_async(strongSelf->_URLRequestQueue, ^{
[task cancel];
task = nil;
});
[strongSelf dequeueTasks];
};
}

Expand All @@ -507,7 +516,7 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
__block volatile uint32_t cancelled = 0;
__block dispatch_block_t cancelLoad = nil;
dispatch_block_t cancellationBlock = ^{
if (cancelLoad) {
if (cancelLoad && !cancelled) {
cancelLoad();
}
OSAtomicOr32Barrier(1, &cancelled);
Expand Down
14 changes: 11 additions & 3 deletions Libraries/Network/RCTNetworkTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ - (void)invalidate
_incrementalDataBlock = nil;
_responseBlock = nil;
_uploadProgressBlock = nil;
_requestToken = nil;
}

- (void)dispatchCallback:(dispatch_block_t)callback
Expand All @@ -66,6 +67,11 @@ - (void)dispatchCallback:(dispatch_block_t)callback

- (void)start
{
if (_status != RCTNetworkTaskPending) {
RCTLogError(@"RCTNetworkTask was already started or completed");
return;
}

if (_requestToken == nil) {
id token = [_handler sendRequest:_request withDelegate:self];
if ([self validateRequestToken:token]) {
Expand All @@ -77,13 +83,15 @@ - (void)start

- (void)cancel
{
if (_status == RCTNetworkTaskInProgress) {
if (_status == RCTNetworkTaskFinished) {
return;
}

_status = RCTNetworkTaskFinished;
id token = _requestToken;
if (token && [_handler respondsToSelector:@selector(cancelRequest:)]) {
[_handler cancelRequest:token];
}
}
_status = RCTNetworkTaskFinished;
[self invalidate];
}

Expand Down

0 comments on commit 80c1f09

Please sign in to comment.