问题
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