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
__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;
@ -488,8 +490,15 @@ static UIImage *RCTResizeImageIfNeeded(UIImage *image,
}
return ^{
[task cancel];
[weakSelf dequeueTasks];
__typeof(self) strongSelf = weakSelf;
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 dispatch_block_t cancelLoad = nil;
dispatch_block_t cancellationBlock = ^{
if (cancelLoad) {
if (cancelLoad && !cancelled) {
cancelLoad();
}
OSAtomicOr32Barrier(1, &cancelled);

View File

@ -53,6 +53,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
_incrementalDataBlock = nil;
_responseBlock = nil;
_uploadProgressBlock = nil;
_requestToken = nil;
}
- (void)dispatchCallback:(dispatch_block_t)callback
@ -66,6 +67,11 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (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]) {
@ -77,6 +83,10 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (void)cancel
{
if (_status == RCTNetworkTaskFinished) {
return;
}
_status = RCTNetworkTaskFinished;
id token = _requestToken;
if (token && [_handler respondsToSelector:@selector(cancelRequest:)]) {