[IOS Question] uploadTaskWithStreamedRequest
Trying to figure out why some uploads fail when they are just a bit large (e.g., 20-25mb). Didn't find the answer, but the following docs brought my attention:
https://developer.apple.com/documentation/foundation/nsurlsession/1410934-uploadtaskwithstreamedrequest?language=objc
A URL request object that provides the URL, cache policy, request type, and so on.
The body stream and body data in this request object are ignored, and the session calls its delegate’s URLSession:task:needNewBodyStream: method to provide the body data.
Current code: https://github.com/Vydia/react-native-background-upload/blob/master/ios/VydiaRNFileUploader.m#L200
However, the current code does not implement the above, but rather set the body on the request. I believe it works nevertheless, but I wonder if it is just luck.
The code docs also say:
/* Creates an upload task with the given request.
The previously set body stream of the request (if any) is ignored and the URLSession:task:needNewBodyStream: delegate will be called when the body payload is required. */
- (NSURLSessionUploadTask *)uploadTaskWithStreamedRequest:(NSURLRequest *)request;
So, I'm wondering if it should be using uploadTaskWithRequest instead. Or even better, implement the URLSession:task:needNewBodyStream: delegate.
Thoughts? My Objective-C knowledge is limited so I might be quite wrong about this.
So after testing, it seems like uploadTaskWithRequest won't work when using background sessions. Still no idea why uploadTaskWithStreamedRequest works (at least with small files), but if we were to upload large multipart requests, it looks like the only way would be to write the multipart request body to a temp file, and use the upload file option instead.
From what I see uploadTaskWithStreamedRequest does work (as expected), it's just that Apple documentation is very misleading...
In uploadTaskWithStreamedRequest page: https://developer.apple.com/documentation/foundation/nsurlsession/1410934-uploadtaskwithstreamedrequest?language=objc
After you create the task, you must start it by calling its resume method. The task calls methods on the session’s delegate to provide you with the upload’s progress, response metadata, response data, and so on. The session’s delegate must have a URLSession:task:needNewBodyStream: method that provides the body data to upload.
In URLSession:task:needNewBodyStream: page: https://developer.apple.com/documentation/foundation/nsurlsessiontaskdelegate/1410001-urlsession?language=objc
Note: You don’t need to implement this method if your code provides the request body using a file URL or a data object.
Interesting, but what about this? https://developer.apple.com/documentation/foundation/url_loading_system/downloading_files_in_the_background?language=objc
Only upload tasks from a file are supported (uploads from data instances or a stream fail after the app exits).
I can't tell if app exits is the same as suspension in this case or not. I know for sure that data tasks will not even start with a background session.