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 theURLSession
object you used when initiating the background request. If your widget extension was terminated, use the identifier to recreate theURLSession
.
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?