URLSessionTask总是会出错 [英] URLSessionTask runs always into an error
问题描述
在我的ExtensionDelegate中,我使用以下代码启动URLSessionTask:
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()
}
我的ExtensionDelegate当然实现了URLSessionDataDelegate. 这总是会出错.意思
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
将被触发,并显示以下错误消息:
URLSessionTask didCompleteWithError可选(错误域= NSURLErrorDomain代码= -997与后台传输服务的连接断开" UserInfo = {NSErrorFailingURLKey = https://api.wedtec.net/cryptocoins/index.php?bitcoin&simple ,NSLocalizedDescription =与后台传输服务的连接断开})
任何想法在这里可能出什么问题吗?从后台进程(WKApplicationRefreshBackgroundTask)中调用scheduleNextURLSessionTask.
根据Apple文档,只能将下载和上传任务安排为作为后台任务执行:
仅支持上传和下载任务(无数据任务)."
在背景传输注意事项"下,此处.
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).
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"-
这篇关于URLSessionTask总是会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!