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.