简体   繁体   English

WidgetKit 不调用 urlSessionDidFinishEvents

[英]WidgetKit not calling urlSessionDidFinishEvents

I'm trying to implement downloading timeline data in a widget and so I created a background URLSession with a corresponding data task to download the JSON:我正在尝试在小部件中实现下载时间线数据,因此我创建了一个具有相应数据任务的后台URLSession来下载 JSON:

let session = URLSession(
  configuration: .background(withIdentifier: identifier),
  delegate: self,
  delegateQueue: nil
)

let request = URLRequest(url: ...)
session.dataTask(with: request).resume()

On my widget I then added the onBackgroundURLSessionEvents to store the completion handler, as per the Apple docs:然后,根据 Apple 文档,在我的小部件上添加了onBackgroundURLSessionEvents来存储完成处理程序:

.onBackgroundURLSessionEvents { identifier in
  return SessionCache.shared.isValid(for: identifier)
} _: { identifier, completion in
  let data = SessionCache.shared.sessionData(for: identifier)
  data.sessionCompletion = completion
}

However, neither the urlSessionDidFinishEvents(forBackgroundURLSession:) nor the onBackgroundURLSessionEvents methods are called.然而, urlSessionDidFinishEvents(forBackgroundURLSession:)onBackgroundURLSessionEvents方法都没有被调用。 When the.network download completes, it just calls the normal urlSession(_:task:didCompleteWithError:) method.当 .network 下载完成时,它只是调用正常的urlSession(_:task:didCompleteWithError:)方法。

I'm clearly missing something here, but I'm just not seeing what.我显然在这里遗漏了一些东西,但我只是没有看到什么。

Apple's documentation isn't 100% clear on this, but you need to use URLSession.downloadTask instead of dataTask for background sessions. Apple 的文档对此并非 100% 清楚,但您需要使用URLSession.downloadTask而不是dataTask进行后台会话。 URLSessionDataTask s deliver bytes to those specific delegate methods in your in-memory process. URLSessionDataTask将字节传递给内存中进程中的那些特定委托方法。 Background download & upload tasks are handed off to nsurlsessiond and only delivered back to your app when they are fully resolved.后台下载和上传任务被移交给nsurlsessiond ,只有在它们完全解决后才会返回给您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM