URLSessionTask总是会出错

URLSessionTask总是会出错

本文介绍了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?)

将被触发,并显示以下错误消息:

任何想法在这里可能出什么问题吗?从后台进程(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:

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总是会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 20:41