Hi folks,
Been working with background uploads. However, I'm not sure about some cases or what is the best way to handle it.
For example: I start an upload task, I kill the app by swiping it off. My understanding is that the task can finish in background, reopen the app, and resume.
The two parts I am not too sure is how to:
1) Upon opening the app getting the running task and reconnecting them instead of recreating new once.
2) Upon app getting relaunched from task completion, get it's response or progress if this happens when app was dead.
If I recreate an NSURLSession with the same identifier I started the tasks before kill the app, will apple api's let me get all the tasks that are still running when I call "getTasksWithCompletionHandler"? And if yes, how do I get them reconnected to my delegate?
What I have is AppDelegate: handleEventsForBackgroundURLSession
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier
completionHandler:(void (^)(void))completionHandler {
NSURLSessionConfiguration* config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier];
NSURLSession* session = [NSURLSession sessionWithConfiguration:config delegate:taskProcessorSingleton delegateQueue:nil];
[session getTasksWithCompletionHandler:^(NSArray<NSURLSessionDataTask *> * _Nonnull dataTasks, NSArray<NSURLSessionUploadTask *> * _Nonnull uploadTasks, NSArray<NSURLSessionDownloadTask *> * _Nonnull downloadTasks) {
//TODO: Can I get responseData for tasks here? Or is this a complete wrong way of looking at it?
// should I expect that delegate here will start getting calls such as "receivedResponse" and "didCompleteWithError"?
completionHandler();
}
Here is how I make tasks in a singleton class and handle responses ... all works well in foreground:
// make session - this is done once in a singleton.
NSString * sessionName = @"backgroundSessionImageUpload";
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:sessionName];
configuration.allowsCellularAccess = YES;
configuration.discretionary = NO;
configuration.sessionSendsLaunchEvents = YES;
self.session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
// make request and task - this is done per upload
NSURLRequest * request = [self createRequest];
NSURLSessionUploadTask* task = [self.session uploadTaskWithRequest:request fromFile:url];
task.taskDescription = [NSString stringWithFormat:@"%ld", self.uploadID]; // so I know which one is which.
// store in local array and start.
[self.tasks addObject:task];
[task resume];
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(nullable NSError *)error {
// handle completion or error... when all is done.
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data {
// response data here...
}
Thank you very much,
Chris