From 04187536d15d8946084aa24cda41dd5705028104 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Wed, 2 Dec 2015 01:27:56 -0800 Subject: [PATCH] Fixed threading bug in RCTNetworking Summary: public When uploading images, RCTHTTPFormDataHelper was sometimes accessed on the wrong thread. Reviewed By: helouree Differential Revision: D2709186 fb-gh-sync-id: d0a14926927d1d41f602f78a9f6892dfbdfc6ff9 --- Libraries/Network/RCTNetworking.m | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Libraries/Network/RCTNetworking.m b/Libraries/Network/RCTNetworking.m index 74b58972c..9afb0914e 100644 --- a/Libraries/Network/RCTNetworking.m +++ b/Libraries/Network/RCTNetworking.m @@ -59,6 +59,8 @@ static NSString *RCTGenerateFormBoundary() - (RCTURLRequestCancellationBlock)process:(NSDictionaryArray *)formData callback:(RCTHTTPQueryResult)callback { + RCTAssertThread(_networker.methodQueue, @"process: must be called on method queue"); + if (formData.count == 0) { return callback(nil, nil); } @@ -76,6 +78,8 @@ static NSString *RCTGenerateFormBoundary() - (RCTURLRequestCancellationBlock)handleResult:(NSDictionary *)result error:(NSError *)error { + RCTAssertThread(_networker.methodQueue, @"handleResult: must be called on method queue"); + if (error) { return _callback(error, nil); } @@ -256,7 +260,7 @@ RCT_EXPORT_MODULE() - (RCTURLRequestCancellationBlock)processDataForHTTPQuery:(nullable NSDictionary *)query callback: (RCTURLRequestCancellationBlock (^)(NSError *error, NSDictionary *result))callback { - RCTAssertThread(_methodQueue, @"processData: must be called on method queue"); + RCTAssertThread(_methodQueue, @"processDataForHTTPQuery: must be called on method queue"); if (!query) { return callback(nil, nil); @@ -270,7 +274,9 @@ RCT_EXPORT_MODULE() __block RCTURLRequestCancellationBlock cancellationBlock = nil; RCTNetworkTask *task = [self networkTaskWithRequest:request completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) { - cancellationBlock = callback(error, data ? @{@"body": data, @"contentType": RCTNullIfNil(response.MIMEType)} : nil); + dispatch_async(_methodQueue, ^{ + cancellationBlock = callback(error, data ? @{@"body": data, @"contentType": RCTNullIfNil(response.MIMEType)} : nil); + }); }]; [task start];