URLSessionTask总是会出错 [英] URLSessionTask runs always into an error

查看:1573
本文介绍了URLSessionTask总是会出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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 = 解决方案

根据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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆