I have an application which would support downloading the content to local disk. Users can choose the items they want to save. After downloading complete, I will unzip the downloaded file and encrypt then save to local. I use NSURLSession with backgroundConfiguration to support background download. I want user to access the downloaded content ASAP, so I implement my own queue to handle the download items. I wish the download mechanism could work both on foreground and background. Here are some mechanisms and their result
- Method 1:
Create each download task first, and enqueue the object
downloadObj.downloadTask = [session downloadTaskWithRequest:request];
downloadObj.taskIdentifier = downloadObj.downloadTask.taskIdentifier;
[Queue enqueue:downloadObj];
Process the head object in the queue
obj = [Queue objectAtIndex:0];
[obj.downloadTask resume];
Handle next object in URLSession delegate function
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)downloadURL {
finishObj = [Queue findObjFromIdentifier:downloadTask.taskIdentifier];
nextObj = [Queue findNextObj:finishObj];
[nextObj.downloadTask resume];
[Queue removeObject:finishObj];
}
This method could work properly when app always in foreground. When the app enter background, all the created downloadTasks seems to resume automatically. So they will share the bandwidth at the same time. It doesn't follow FIFO....
- Method 2:
Create the download task in URLSession delegate function, and resume directly
This method only download the running download task when app has already entered background.
Anyone can give me some advise about the background with First In First Out property?