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:
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:
.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. When the network download completes, it just calls the normal urlSession(_:task:didCompleteWithError:)
method.
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 ofdataTask
for background sessions.URLSessionDataTask
s deliver bytes to those specific delegate methods in your in-memory process. Background download & upload tasks are handed off tonsurlsessiond
and only delivered back to your app when they are fully resolved.