0

I have this scenario in my app:

I have to execute 2 block of code repeatedly for N times.

in the first block I have to do a network call with Alamofire (I call a HTTP service). this call returns data and in this data there is a image path that I have to download on device

in the second block I have to download the image and then save it on device

After this start over again for N times.

My problem is that the user can have app in background and I would to like that this process continues when app is in background too.

For this reason, after the Alamofire call, I insert this code for download image:

let config = URLSessionConfiguration.background(withIdentifier: "XXXXXX.XXXX")
let session = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue())
let url = URL(string: "testurl")
let task = session.downloadTask(with: url!)
task.resume()


func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {

    //save image on device and I recall Alamofire for the next call
}

My problem is this: this process doesent execute when app goes in background. What I'm wrong?

gianni rodari
  • 117
  • 3
  • 7

1 Answers1

0

You need to implement the below delegate,

func application(_ application: UIApplication,
             handleEventsForBackgroundURLSession identifier: String,
             completionHandler: @escaping () -> Void) {
    // send the completion handler as background completion handler
}

Please refer the below answer for more information: https://stackoverflow.com/a/44140059/1244403

Harish
  • 2,496
  • 4
  • 24
  • 48