Avoid dispatching network callbacks when already on the right queue

Reviewed By: mmmulani

Differential Revision: D3690133

fbshipit-source-id: a1769016a05b3163a3b1c93f448864aeaecc6e46
This commit is contained in:
Pieter De Baets 2016-08-17 10:34:16 -07:00 committed by Facebook Github Bot 7
parent 66bea7d1e5
commit 0b1954cace
2 changed files with 28 additions and 12 deletions

View File

@ -9,6 +9,8 @@
#import "RCTHTTPRequestHandler.h" #import "RCTHTTPRequestHandler.h"
#import "RCTNetworking.h"
@interface RCTHTTPRequestHandler () <NSURLSessionDataDelegate> @interface RCTHTTPRequestHandler () <NSURLSessionDataDelegate>
@end @end
@ -19,6 +21,8 @@
NSURLSession *_session; NSURLSession *_session;
} }
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE() RCT_EXPORT_MODULE()
- (void)invalidate - (void)invalidate
@ -55,6 +59,7 @@ RCT_EXPORT_MODULE()
NSOperationQueue *callbackQueue = [NSOperationQueue new]; NSOperationQueue *callbackQueue = [NSOperationQueue new];
callbackQueue.maxConcurrentOperationCount = 1; callbackQueue.maxConcurrentOperationCount = 1;
callbackQueue.underlyingQueue = [[_bridge networking] methodQueue];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
_session = [NSURLSession sessionWithConfiguration:configuration _session = [NSURLSession sessionWithConfiguration:configuration
delegate:self delegate:self

View File

@ -37,6 +37,8 @@
_handler = handler; _handler = handler;
_callbackQueue = callbackQueue; _callbackQueue = callbackQueue;
_status = RCTNetworkTaskPending; _status = RCTNetworkTaskPending;
dispatch_queue_set_specific(callbackQueue, (__bridge void *)self, (__bridge void *)self, NULL);
} }
return self; return self;
} }
@ -53,6 +55,15 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
_uploadProgressBlock = nil; _uploadProgressBlock = nil;
} }
- (void)dispatchCallback:(dispatch_block_t)callback
{
if (dispatch_get_specific((__bridge void *)self) == (__bridge void *)self) {
callback();
} else {
dispatch_async(_callbackQueue, callback);
}
}
- (void)start - (void)start
{ {
if (_requestToken == nil) { if (_requestToken == nil) {
@ -96,9 +107,9 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
_status = RCTNetworkTaskFinished; _status = RCTNetworkTaskFinished;
if (_completionBlock) { if (_completionBlock) {
RCTURLRequestCompletionBlock completionBlock = _completionBlock; RCTURLRequestCompletionBlock completionBlock = _completionBlock;
dispatch_async(_callbackQueue, ^{ [self dispatchCallback:^{
completionBlock(self->_response, nil, RCTErrorWithMessage(@"Invalid request token.")); completionBlock(self->_response, nil, RCTErrorWithMessage(@"Invalid request token."));
}); }];
} }
[self invalidate]; [self invalidate];
} }
@ -114,9 +125,9 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
if (_uploadProgressBlock) { if (_uploadProgressBlock) {
RCTURLRequestProgressBlock uploadProgressBlock = _uploadProgressBlock; RCTURLRequestProgressBlock uploadProgressBlock = _uploadProgressBlock;
int64_t length = _request.HTTPBody.length; int64_t length = _request.HTTPBody.length;
dispatch_async(_callbackQueue, ^{ [self dispatchCallback:^{
uploadProgressBlock(bytesSent, length); uploadProgressBlock(bytesSent, length);
}); }];
} }
} }
@ -129,9 +140,9 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
_response = response; _response = response;
if (_responseBlock) { if (_responseBlock) {
RCTURLRequestResponseBlock responseBlock = _responseBlock; RCTURLRequestResponseBlock responseBlock = _responseBlock;
dispatch_async(_callbackQueue, ^{ [self dispatchCallback:^{
responseBlock(response); responseBlock(response);
}); }];
} }
} }
@ -151,15 +162,15 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
if (_incrementalDataBlock) { if (_incrementalDataBlock) {
RCTURLRequestIncrementalDataBlock incrementalDataBlock = _incrementalDataBlock; RCTURLRequestIncrementalDataBlock incrementalDataBlock = _incrementalDataBlock;
dispatch_async(_callbackQueue, ^{ [self dispatchCallback:^{
incrementalDataBlock(data, length, total); incrementalDataBlock(data, length, total);
}); }];
} }
if (_downloadProgressBlock && total > 0) { if (_downloadProgressBlock && total > 0) {
RCTURLRequestProgressBlock downloadProgressBlock = _downloadProgressBlock; RCTURLRequestProgressBlock downloadProgressBlock = _downloadProgressBlock;
dispatch_async(_callbackQueue, ^{ [self dispatchCallback:^{
downloadProgressBlock(length, total); downloadProgressBlock(length, total);
}); }];
} }
} }
@ -172,9 +183,9 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
_status = RCTNetworkTaskFinished; _status = RCTNetworkTaskFinished;
if (_completionBlock) { if (_completionBlock) {
RCTURLRequestCompletionBlock completionBlock = _completionBlock; RCTURLRequestCompletionBlock completionBlock = _completionBlock;
dispatch_async(_callbackQueue, ^{ [self dispatchCallback:^{
completionBlock(self->_response, self->_data, error); completionBlock(self->_response, self->_data, error);
}); }];
} }
[self invalidate]; [self invalidate];
} }