Skip to content

Commit b06b552

Browse files
javachemikelambert
authored andcommitted
Cancel network requests from the correct queue
Summary: Fix suggested by sooth-sayer (facebook#10280) Reviewed By: mmmulani Differential Revision: D4001618 fbshipit-source-id: cc28d19d02a29b62d2bdbddcd30f94b1c1bcfd76
1 parent 245cb81 commit b06b552

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

Libraries/Image/RCTImageLoader.m

+13-4
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,9 @@ - (RCTImageLoaderCancellationBlock)_loadURLRequest:(NSURLRequest *)request
440440

441441
// Download image
442442
__weak __typeof(self) weakSelf = self;
443-
RCTNetworkTask *task = [networking networkTaskWithRequest:request completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) {
443+
__block RCTNetworkTask *task =
444+
[networking networkTaskWithRequest:request
445+
completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) {
444446
__typeof(self) strongSelf = weakSelf;
445447
if (!strongSelf) {
446448
return;
@@ -480,8 +482,15 @@ - (RCTImageLoaderCancellationBlock)_loadURLRequest:(NSURLRequest *)request
480482
}
481483

482484
return ^{
483-
[task cancel];
484-
[weakSelf dequeueTasks];
485+
__typeof(self) strongSelf = weakSelf;
486+
if (!strongSelf || !task) {
487+
return;
488+
}
489+
dispatch_async(strongSelf->_URLRequestQueue, ^{
490+
[task cancel];
491+
task = nil;
492+
});
493+
[strongSelf dequeueTasks];
485494
};
486495
}
487496

@@ -496,7 +505,7 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
496505
__block volatile uint32_t cancelled = 0;
497506
__block dispatch_block_t cancelLoad = nil;
498507
dispatch_block_t cancellationBlock = ^{
499-
if (cancelLoad) {
508+
if (cancelLoad && !cancelled) {
500509
cancelLoad();
501510
}
502511
OSAtomicOr32Barrier(1, &cancelled);

Libraries/Network/RCTNetworkTask.m

+10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ - (void)invalidate
5353
_incrementalDataBlock = nil;
5454
_responseBlock = nil;
5555
_uploadProgressBlock = nil;
56+
_requestToken = nil;
5657
}
5758

5859
- (void)dispatchCallback:(dispatch_block_t)callback
@@ -66,6 +67,11 @@ - (void)dispatchCallback:(dispatch_block_t)callback
6667

6768
- (void)start
6869
{
70+
if (_status != RCTNetworkTaskPending) {
71+
RCTLogError(@"RCTNetworkTask was already started or completed");
72+
return;
73+
}
74+
6975
if (_requestToken == nil) {
7076
id token = [_handler sendRequest:_request withDelegate:self];
7177
if ([self validateRequestToken:token]) {
@@ -77,6 +83,10 @@ - (void)start
7783

7884
- (void)cancel
7985
{
86+
if (_status == RCTNetworkTaskFinished) {
87+
return;
88+
}
89+
8090
_status = RCTNetworkTaskFinished;
8191
id token = _requestToken;
8292
if (token && [_handler respondsToSelector:@selector(cancelRequest:)]) {

0 commit comments

Comments
 (0)