Hello. I'm using NSURLSession (with background configuration) for downloading files. I have couple questions about NSURLSession workflow.
1) Sometimes, in very rare cases I noticed that in callbacks
task.repsonse / task.originalRequest has nil value. Unfortunately, I can't 100% reproduce it. I suggest that it can depends on some backend configuration or some edge case while file is downloading.
In docs these fields marked as nullable (so seems it's correct behavior), but explanation is very poor.
For example, for originalRequest it says
and task usually contains originalRequest (at least when I created it).
2) In most samples, people (and I'm) using originalRequest as a unique key for tracking process (updating UI, moving downloaded file to documents directory, etc) and task.repsonse for detecting status code. So these fields are critical for proper management.
How to properly handle situation when these fields are nil? Can I just ignore it? Is it safe behavior?
3) Another interesting example which I noticed from different downloader implementations - save source URL and other metadata as task.taskDescription and use it instead of originalRequest in callbacks. Main advantage here that I can fetch tasks after app relaunch using
method. Is it a safe approach? I think to it in case when originalRequest is nil.
1) Sometimes, in very rare cases I noticed that in callbacks
Code Block - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
task.repsonse / task.originalRequest has nil value. Unfortunately, I can't 100% reproduce it. I suggest that it can depends on some backend configuration or some edge case while file is downloading.
In docs these fields marked as nullable (so seems it's correct behavior), but explanation is very poor.
For example, for originalRequest it says
But I'm not using stream task. I usemay be nil if this is a stream task.
Code Block [self.session downloadTaskWithURL:someURL];
and task usually contains originalRequest (at least when I created it).
2) In most samples, people (and I'm) using originalRequest as a unique key for tracking process (updating UI, moving downloaded file to documents directory, etc) and task.repsonse for detecting status code. So these fields are critical for proper management.
How to properly handle situation when these fields are nil? Can I just ignore it? Is it safe behavior?
3) Another interesting example which I noticed from different downloader implementations - save source URL and other metadata as task.taskDescription and use it instead of originalRequest in callbacks. Main advantage here that I can fetch tasks after app relaunch using
Code Block - (void)getTasksWithCompletionHandler:(void (^)(NSArray<NSURLSessionDataTask *> *dataTasks, NSArray<NSURLSessionUploadTask *> *uploadTasks, NSArray<NSURLSessionDownloadTask *> *downloadTasks))completionHandler;
method. Is it a safe approach? I think to it in case when originalRequest is nil.