Cancel network requests from the correct queue

Summary: Fix suggested by sooth-sayer (https://github.com/facebook/react-native/pull/10280)

Reviewed By: mmmulani

Differential Revision: D4001618

fbshipit-source-id: cc28d19d02a29b62d2bdbddcd30f94b1c1bcfd76
This commit is contained in:
Pieter De Baets 2016-10-11 12:34:58 -07:00 committed by Facebook Github Bot
parent 84eaeb0adf
commit 26be005b0a
2 changed files with 23 additions and 4 deletions

View File

@ -444,7 +444,9 @@ static UIImage *RCTResizeImageIfNeeded(UIImage *image,
// Download image // Download image
__weak __typeof(self) weakSelf = self; __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; __typeof(self) strongSelf = weakSelf;
if (!strongSelf) { if (!strongSelf) {
return; return;
@ -488,8 +490,15 @@ static UIImage *RCTResizeImageIfNeeded(UIImage *image,
} }
return ^{ return ^{
[task cancel]; __typeof(self) strongSelf = weakSelf;
[weakSelf dequeueTasks]; if (!strongSelf || !task) {
return;
}
dispatch_async(strongSelf->_URLRequestQueue, ^{
[task cancel];
task = nil;
});
[strongSelf dequeueTasks];
}; };
} }
@ -505,7 +514,7 @@ static UIImage *RCTResizeImageIfNeeded(UIImage *image,
__block volatile uint32_t cancelled = 0; __block volatile uint32_t cancelled = 0;
__block dispatch_block_t cancelLoad = nil; __block dispatch_block_t cancelLoad = nil;
dispatch_block_t cancellationBlock = ^{ dispatch_block_t cancellationBlock = ^{
if (cancelLoad) { if (cancelLoad && !cancelled) {
cancelLoad(); cancelLoad();
} }
OSAtomicOr32Barrier(1, &cancelled); OSAtomicOr32Barrier(1, &cancelled);

View File

@ -53,6 +53,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
_incrementalDataBlock = nil; _incrementalDataBlock = nil;
_responseBlock = nil; _responseBlock = nil;
_uploadProgressBlock = nil; _uploadProgressBlock = nil;
_requestToken = nil;
} }
- (void)dispatchCallback:(dispatch_block_t)callback - (void)dispatchCallback:(dispatch_block_t)callback
@ -66,6 +67,11 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (void)start - (void)start
{ {
if (_status != RCTNetworkTaskPending) {
RCTLogError(@"RCTNetworkTask was already started or completed");
return;
}
if (_requestToken == nil) { if (_requestToken == nil) {
id token = [_handler sendRequest:_request withDelegate:self]; id token = [_handler sendRequest:_request withDelegate:self];
if ([self validateRequestToken:token]) { if ([self validateRequestToken:token]) {
@ -77,6 +83,10 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (void)cancel - (void)cancel
{ {
if (_status == RCTNetworkTaskFinished) {
return;
}
_status = RCTNetworkTaskFinished; _status = RCTNetworkTaskFinished;
id token = _requestToken; id token = _requestToken;
if (token && [_handler respondsToSelector:@selector(cancelRequest:)]) { if (token && [_handler respondsToSelector:@selector(cancelRequest:)]) {