Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ - (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequ

__weak __block NSBlockOperation *weakOp;
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
// [macOS
NSBlockOperation *strongOp = weakOp; // Strong reference to avoid deallocation during execution
if (strongOp == nil || [strongOp isCancelled]) {
return;
}
// macOS]

// Get mime type
NSRange firstSemicolon = [request.URL.resourceSpecifier rangeOfString:@";"];
NSString *mimeType =
Expand All @@ -51,15 +58,15 @@ - (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequ
expectedContentLength:-1
textEncodingName:nil];

[delegate URLRequest:weakOp didReceiveResponse:response];
[delegate URLRequest:strongOp didReceiveResponse:response]; // [macOS]

// Load data
NSError *error;
NSData *data = [NSData dataWithContentsOfURL:request.URL options:NSDataReadingMappedIfSafe error:&error];
if (data) {
[delegate URLRequest:weakOp didReceiveData:data];
[delegate URLRequest:strongOp didReceiveData:data]; // [macOS]
}
[delegate URLRequest:weakOp didCompleteWithError:error];
[delegate URLRequest:strongOp didCompleteWithError:error]; // [macOS]
}];

weakOp = op;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,19 @@ - (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequ

__weak __block NSBlockOperation *weakOp;
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
// [macOS
NSBlockOperation *strongOp = weakOp; // Strong reference to avoid deallocation during execution
if (strongOp == nil || [strongOp isCancelled]) {
return;
}
// macOS]

// Get content length
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager new];
NSDictionary<NSString *, id> *fileAttributes = [fileManager attributesOfItemAtPath:request.URL.path error:&error];
if (!fileAttributes) {
[delegate URLRequest:weakOp didCompleteWithError:error];
[delegate URLRequest:strongOp didCompleteWithError:error]; // [macOS]
return;
}

Expand All @@ -70,14 +77,14 @@ - (NSOperation *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequ
expectedContentLength:[fileAttributes[NSFileSize] ?: @-1 integerValue]
textEncodingName:nil];

[delegate URLRequest:weakOp didReceiveResponse:response];
[delegate URLRequest:strongOp didReceiveResponse:response]; // [macOS]

// Load data
NSData *data = [NSData dataWithContentsOfURL:request.URL options:NSDataReadingMappedIfSafe error:&error];
if (data) {
[delegate URLRequest:weakOp didReceiveData:data];
[delegate URLRequest:strongOp didReceiveData:data]; // [macOS]
}
[delegate URLRequest:weakOp didCompleteWithError:error];
[delegate URLRequest:strongOp didCompleteWithError:error]; // [macOS]
}];

weakOp = op;
Expand Down