URLSessionTask runs always into an error

怎甘沉沦 提交于 2019-12-22 10:11:47

问题


In my ExtensionDelegate I'm starting a URLSessionTask with following code:

func scheduleNextURLSessionTask() {
    let backgroundConfigObject = URLSessionConfiguration.background(withIdentifier: "myIdentifier")

    let backgroundSession = URLSession(configuration: backgroundConfigObject, delegate: self, delegateQueue: nil)
    let retrieveTask = backgroundSession.dataTask(with: URL(string: "https://api.wedtec.net/cryptocoins/index.php?bitcoin&simple")!)
    retrieveTask.resume()
}

My ExtensionDelegate implements URLSessionDataDelegate, of course. This runs always into an error. Means

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)

Will be triggered will following error message:

URLSessionTask didCompleteWithError Optional(Error Domain=NSURLErrorDomain Code=-997 "Lost connection to background transfer service" UserInfo={NSErrorFailingURLKey=https://api.wedtec.net/cryptocoins/index.php?bitcoin&simple, NSErrorFailingURLStringKey=https://api.wedtec.net/cryptocoins/index.php?bitcoin&simple, NSLocalizedDescription=Lost connection to background transfer service})

Any Idea what could be wrong here? scheduleNextURLSessionTask is called from a background process (WKApplicationRefreshBackgroundTask).


回答1:


According to Apple documentation, only download and upload tasks can be scheduled for executing as background tasks:

"Only upload and download tasks are supported (no data tasks)."

Check here, under "Background transfer considerations"-



来源:https://stackoverflow.com/questions/39390388/urlsessiontask-runs-always-into-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!