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
  • Loading branch information
javache authored and oblador committed Oct 13, 2016
1 parent 3b1ba29 commit 00feff3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
17 changes: 13 additions & 4 deletions Libraries/Image/RCTImageLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,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 @@ -488,8 +490,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 @@ -505,7 +514,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
10 changes: 10 additions & 0 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,6 +83,10 @@ - (void)start

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

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

0 comments on commit 00feff3

Please sign in to comment.