From 925acc45b6d75aa5ced1417c995ee6ff05ce49cd Mon Sep 17 00:00:00 2001 From: Phillip Pan Date: Wed, 4 Oct 2023 11:53:59 -0700 Subject: [PATCH] explicitly dispatch async RCTNetworking methods Differential Revision: D49693966 fbshipit-source-id: d7d031a41c76f5f8b3b6d4813f7b44b104f7610c --- .../Libraries/Network/RCTNetworking.mm | 81 +++++++++++-------- 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/packages/react-native/Libraries/Network/RCTNetworking.mm b/packages/react-native/Libraries/Network/RCTNetworking.mm index 4e78d8e798e8ae..7abc90d75c2ead 100644 --- a/packages/react-native/Libraries/Network/RCTNetworking.mm +++ b/packages/react-native/Libraries/Network/RCTNetworking.mm @@ -700,49 +700,64 @@ - (RCTNetworkTask *)networkTaskWithRequest:(NSURLRequest *)request : (JS::NativeNetworkingIOS::SpecSendRequestQuery &)query callback : (RCTResponseSenderBlock)responseSender) { - NSDictionary *queryDict = @{ - @"method" : query.method(), - @"url" : query.url(), - @"data" : query.data(), - @"headers" : query.headers(), - @"responseType" : query.responseType(), - @"incrementalUpdates" : @(query.incrementalUpdates()), - @"timeout" : @(query.timeout()), - @"withCredentials" : @(query.withCredentials()), - }; + NSString *method = query.method(); + NSString *url = query.url(); + id data = query.data(); + id headers = query.headers(); + NSString *queryResponseType = query.responseType(); + bool queryIncrementalUpdates = query.incrementalUpdates(); + double timeout = query.timeout(); + bool withCredentials = query.withCredentials(); + + dispatch_async(_methodQueue, ^{ + NSDictionary *queryDict = @{ + @"method" : method, + @"url" : url, + @"data" : data, + @"headers" : headers, + @"responseType" : queryResponseType, + @"incrementalUpdates" : @(queryIncrementalUpdates), + @"timeout" : @(timeout), + @"withCredentials" : @(withCredentials), + }; - // TODO: buildRequest returns a cancellation block, but there's currently - // no way to invoke it, if, for example the request is cancelled while - // loading a large file to build the request body - [self buildRequest:queryDict - completionBlock:^(NSURLRequest *request) { - NSString *responseType = [RCTConvert NSString:queryDict[@"responseType"]]; - BOOL incrementalUpdates = [RCTConvert BOOL:queryDict[@"incrementalUpdates"]]; - [self sendRequest:request - responseType:responseType - incrementalUpdates:incrementalUpdates - responseSender:responseSender]; - }]; + // TODO: buildRequest returns a cancellation block, but there's currently + // no way to invoke it, if, for example the request is cancelled while + // loading a large file to build the request body + [self buildRequest:queryDict + completionBlock:^(NSURLRequest *request) { + NSString *responseType = [RCTConvert NSString:queryDict[@"responseType"]]; + BOOL incrementalUpdates = [RCTConvert BOOL:queryDict[@"incrementalUpdates"]]; + [self sendRequest:request + responseType:responseType + incrementalUpdates:incrementalUpdates + responseSender:responseSender]; + }]; + }); } RCT_EXPORT_METHOD(abortRequest : (double)requestID) { - [_tasksByRequestID[[NSNumber numberWithDouble:requestID]] cancel]; - [_tasksByRequestID removeObjectForKey:[NSNumber numberWithDouble:requestID]]; + dispatch_async(_methodQueue, ^{ + [self->_tasksByRequestID[[NSNumber numberWithDouble:requestID]] cancel]; + [self->_tasksByRequestID removeObjectForKey:[NSNumber numberWithDouble:requestID]]; + }); } RCT_EXPORT_METHOD(clearCookies : (RCTResponseSenderBlock)responseSender) { - NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; - if (!storage.cookies.count) { - responseSender(@[ @NO ]); - return; - } + dispatch_async(_methodQueue, ^{ + NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; + if (!storage.cookies.count) { + responseSender(@[ @NO ]); + return; + } - for (NSHTTPCookie *cookie in storage.cookies) { - [storage deleteCookie:cookie]; - } - responseSender(@[ @YES ]); + for (NSHTTPCookie *cookie in storage.cookies) { + [storage deleteCookie:cookie]; + } + responseSender(@[ @YES ]); + }); } - (std::shared_ptr)getTurboModule: