Use weakSelf in RCTNetworking callbacks

Reviewed By: shergin

Differential Revision: D4551000

fbshipit-source-id: fcfce41adb8fd76343b973339ffe1cac0bf8a756
This commit is contained in:
Pieter De Baets 2017-02-13 12:21:38 -08:00 committed by Facebook Github Bot
parent 4695508826
commit 7d51580479
1 changed files with 13 additions and 10 deletions

View File

@ -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<id> *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<id> *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];