colbrew
colbrew

Reputation: 89

In WidgetKit, how do I set a default intent value from dynamic options obtained via a network call?

When a user first installs my widget, when they select "Edit Widget", I'd like a default choice to show up instead of "Choose." This is a dynamic intent and the options are retrieved from a network call.

In my IntentHandler, there are two functions I am playing with:

extension IntentHandler: MyIntentHandling {
    func provideMyValueOptionsCollection(for intent: MyIntent, with completion: @escaping (INObjectCollection<MyValue>?, Error?) -> Void) { }

    func defaultMyValue(for intent: MyIntent) -> [MyValue]? { }
}

I can get the options via a network call for provideMyValueOptionsCollection by calling the completion handler once the values have been retrieved. But defaultMyValue is synchronous, so it won't accept a closure return value from a network call (get error: Cannot convert return expression of type '()' to return type '[MyValue]'). Is there a way to provide a default value obtained from a network call to the Edit Widget screen?

Upvotes: 4

Views: 1381

Answers (1)

Rakuyo
Rakuyo

Reputation: 630

I had the same problem and like you ended up solving it by storing the data in UserDefaults and also had the same problem of not displaying the data when it was first shown

I solved this problem with the following solution:

Remove the time-consuming operation in the defaultMyValue method

We had the operation of converting images to binary data (image url string -> URL -> Data), which was perfectly solved when we moved this operation from the defaultMyValue method to somewhere else (e.g. in the UI).

Upvotes: 1

Related Questions