URLSessionTask runs always into an error

2019-05-10 08:40发布

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条回答
Juvenile、少年°
2楼-- · 2019-05-10 08:54

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"-

查看更多
登录 后发表回答