1

I use URLSession and URLSessionDataTask to get an image from server and display it in an app. I had a look here. It looks like URLSessionDownloadTask has more options.

Currently I use the following code for getting the image:

let task = URLSession.shared.dataTask(with: url) {(data, response, error) in

    guard error == nil else {
        completion(error, nil)
        return
    }

    completion(nil, data)            
}        
task.resume()

I'd like to be able to suspend, cancel and resume the process of getting the image from the server. I see in the documentation that URLSessionDataTask also has these options. But it's also written for the suspend method of URLSessionTask that:

A download task can continue transferring data at a later time. All other tasks must start over when resumed.

So my question is: Should I change the implementation to use URLSessionDownloadTask for getting the images if I need to be able to stop getting the image at some point and resume later without losing the current progress? Thank you in advance.

surToTheW
  • 614
  • 6
  • 25

2 Answers2

3

Yes, if you want to resume a suspended task, a download task lets it resume from where it left off.

Other reasons you might want to use download tasks include that:

  • the peak memory usage is lower, as download tasks write data to the temporary file as it goes along, whereas data tasks hold the complete resource in memory; and

  • with download tasks, you can use URLSessionConfiguration.background so that the download continues even after the user leaves your app.

Rob
  • 371,891
  • 67
  • 713
  • 902
2

NSURLSessionDataTask : Data tasks exchange data using NSData. NSURLSessionDataTask is not supported in Background Sessions because it does not write the content in a form of a local file(stored in memory). Therefore it can not be resumed later onwards .

NSURLSessionDownloadTask : NSURLSessionDownloadTask directly writes the response data to a temporary file. It supports background downloads when the app is not running and in your case does allow resume of downloads.

The question is why do you want to resume the download of an "image" you are trying to display in the App . Will it ever change ? or will it always be the same through out the App. If it has a chance of changing in the future i think you should stick to URLSessionDataTask because imo it will eat up local storage to download and write images over and over again.

The risk of using NSURLSessionDownloadTask is that prior to download you will have to check if the download space available on the device is sufficient to go ahead with , in other words it is a must to handle fileSize errors as apple thinks its upto the developer to meet those requirements

Anjula S.
  • 586
  • 5
  • 17
  • Thank you for your reply! The use case is that the user slides and the next image is shown but the user can come back to the previous image. The user can also slide very fast and not wait for the image to display, but a request will be sent anyway, so why not save what's downloaded until now for later. You're probably right that with many downloads many files will pile up - this is what you meant, right? Maybe I can set a limit somewhere and oldest downloads could be removed? – surToTheW Apr 15 '20 at 17:11
  • You are right , in. that. case you will have to check each time you download about the storage. available to make sure you dont get `diskfull` error while downloading and given that you know the. sizes of. the images you are. downloading , you should refer to this for any. error handlings :) https://stackoverflow.com/questions/55304446/swift-how-to-catch-disk-full-error-on-background-urlsession-downloadtask . Let me know what you think – Anjula S. Apr 15 '20 at 17:55
  • Alright - thank you! So I can check if there is a need to clean up. I wonder what would I clean up. I mean can I find the location of the temporary files to clear them when they take too much space? Or is my only option to just stop saving the partial downloads at this point? – surToTheW Apr 15 '20 at 18:42
  • So I guess if I just call cancel on the unfinished download task it will clear up the resumeData... If that's correct, please update your answer to reflect that handling of the low disk space scenario is needed and possibly with the link you provided, and I'll accept it. – surToTheW Apr 16 '20 at 08:26