Search code examples
watchoswidgetkitwatchos-9

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:

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.


Solution

  • Apple's documentation isn't 100% clear on this, but you need to use URLSession.downloadTask instead of dataTask for background sessions. URLSessionDataTasks deliver bytes to those specific delegate methods in your in-memory process. Background download & upload tasks are handed off to nsurlsessiond and only delivered back to your app when they are fully resolved.