在后台运行带有应用程序的newtork代码

问题描述 投票:0回答:1

我的应用程序中有这种情况:

我必须重复执行2个代码块N次。

在第一步中,我必须使用Alamofire进行网络调用(我称为HTTP服务)。此调用返回数据,并且此数据中有一个我必须在设备上下载的图像路径

在第二个块中,我必须下载图像,然后将其保存在设备上

此后重新开始N次。

我的问题是用户可以在后台运行应用程序,我希望当应用程序也在后台运行时,该过程也可以继续。

由于这个原因,在Alamofire通话之后,我插入以下代码以下载图像:

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
}

我的问题是:当应用程序进入后台时,此过程不会执行。我错了吗?

ios swift background alamofire nsurlsessiondownloadtask
1个回答
0
投票

您需要实现以下委托,

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

请参考以下答案以获取更多信息:https://stackoverflow.com/a/44140059/1244403

© www.soinside.com 2019 - 2024. All rights reserved.