10

I'm trying to understand Apple's documentation for Widget background network requests.

To handle the result of the network request, use the onBackgroundURLSessionEvents(matching:_:) modifier to your widget’s configuration, and do the following:

  • Store a reference to the completion parameter. You call the completion handler after processing all network events.
  • Use the identifier parameter to find the URLSession object you used when initiating the background request. If your widget extension was terminated, use the identifier to recreate the URLSession.

Regarding the first bullet point, what completion parameter is referred to here? Is it the completion parameter of the onBackgroundURLSessionEvents(matching:_:) func or could it possibly be the completion handler of IntentTimelineProvider's getTimeline(for:in:completion:)? If it's the former, wouldn't that mean storing multiple completion handlers in case there are multiple events? And what is the purpose of invoking that completion handler?

For the second bullet-point, how do you use the identifier to recreate a URLSession? In getTimeline(for:in:completion:) should you be saving the configuration to disk to be accessed later when recreating the session?

To refresh your widget’s timeline after the network request completes, call the WidgetCenter methods from your delegate’s implementation of urlSessionDidFinishEvents.

In this part, it seems that they are referring to calling the method WidgetCenter.shared.reloadTimelines(ofKind:). However, won't this trigger a refresh of all instances of your widget? Or does the system know to only refresh the one that called it?

Gil Birman
  • 35,242
  • 14
  • 75
  • 119
  • The completion handle is the second parameter passed to your `urlSessionEvent:` closure (The closure you pass as the second parameter to `onBackgroundURLSessionEvents(matching:_:)`) – Paulw11 Oct 16 '20 at 22:12
  • You don't use the `identifier` to recreate the session as such; You use the identifier to try and retrieve an existing session you have saved, but your code might have been relaunched and the session is no longer in memory, in which case you need to create one\ – Paulw11 Oct 16 '20 at 22:15
  • @GilBirman did you ever figure out how to correctly use background downloads with a widget? – Tom Hamming Jan 05 '21 at 16:35

0 Answers0