From 7d51580479118c499164618d0a226c5212716973 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Mon, 13 Feb 2017 12:21:38 -0800 Subject: [PATCH] Use weakSelf in RCTNetworking callbacks Reviewed By: shergin Differential Revision: D4551000 fbshipit-source-id: fcfce41adb8fd76343b973339ffe1cac0bf8a756 --- Libraries/Network/RCTNetworking.mm | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Libraries/Network/RCTNetworking.mm b/Libraries/Network/RCTNetworking.mm index 0f6b64557..67379f96b 100644 --- a/Libraries/Network/RCTNetworking.mm +++ b/Libraries/Network/RCTNetworking.mm @@ -431,11 +431,11 @@ RCT_EXPORT_MODULE() responseSender:(RCTResponseSenderBlock)responseSender { RCTAssertThread(_methodQueue, @"sendRequest: must be called on method queue"); - + __weak __typeof(self) weakSelf = self; __block RCTNetworkTask *task; RCTURLRequestProgressBlock uploadProgressBlock = ^(int64_t progress, int64_t total) { NSArray *responseJSON = @[task.requestID, @((double)progress), @((double)total)]; - [self sendEventWithName:@"didSendNetworkData" body:responseJSON]; + [weakSelf sendEventWithName:@"didSendNetworkData" body:responseJSON]; }; RCTURLRequestResponseBlock responseBlock = ^(NSURLResponse *response) { @@ -451,7 +451,7 @@ RCT_EXPORT_MODULE() } id responseURL = response.URL ? response.URL.absoluteString : [NSNull null]; NSArray *responseJSON = @[task.requestID, @(status), headers, responseURL]; - [self sendEventWithName:@"didReceiveNetworkResponse" body:responseJSON]; + [weakSelf sendEventWithName:@"didReceiveNetworkResponse" body:responseJSON]; }; // XHR does not allow you to peek at xhr.response before the response is @@ -484,32 +484,35 @@ RCT_EXPORT_MODULE() @(progress + initialCarryLength - incrementalDataCarry.length), @(total)]; - [self sendEventWithName:@"didReceiveNetworkIncrementalData" body:responseJSON]; + [weakSelf sendEventWithName:@"didReceiveNetworkIncrementalData" body:responseJSON]; }; } else { downloadProgressBlock = ^(int64_t progress, int64_t total) { NSArray *responseJSON = @[task.requestID, @(progress), @(total)]; - [self sendEventWithName:@"didReceiveNetworkDataProgress" body:responseJSON]; + [weakSelf sendEventWithName:@"didReceiveNetworkDataProgress" body:responseJSON]; }; } } RCTURLRequestCompletionBlock completionBlock = ^(NSURLResponse *response, NSData *data, NSError *error) { + __typeof(self) strongSelf = weakSelf; + if (!strongSelf) { + return; + } + // Unless we were sending incremental (text) chunks to JS, all along, now // is the time to send the request body to JS. if (!(incrementalUpdates && [responseType isEqualToString:@"text"])) { - [self sendData:data - responseType:responseType - forTask:task]; + [strongSelf sendData:data responseType:responseType forTask:task]; } NSArray *responseJSON = @[task.requestID, RCTNullIfNil(error.localizedDescription), error.code == kCFURLErrorTimedOut ? @YES : @NO ]; - [self sendEventWithName:@"didCompleteNetworkResponse" body:responseJSON]; - [self->_tasksByRequestID removeObjectForKey:task.requestID]; + [strongSelf sendEventWithName:@"didCompleteNetworkResponse" body:responseJSON]; + [strongSelf->_tasksByRequestID removeObjectForKey:task.requestID]; }; task = [self networkTaskWithRequest:request completionBlock:completionBlock];