0

I'm building an iOS widget that performs a network call and displays some data to the user. The data changes over time so I want the widget to update once every hour or so. This is my code for the timeline provider:

func getTimeline(for configuration: SelectChannelIntent, in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> Void) {
    Task {
        let result = try await fetchData(for: configuration.channel)

        let timeline = Timeline(
            entries: [result],
            policy: .after(Calendar.current.date(byAdding: .minute, value: 60, to: Date())!)
        )

        completion(timeline)
    }
}

Testing this on a physical device resulted in the widget being updated at completely random times. Usually it updated between every 15-45 minutes. I don't want it to update that frequently since the data doesn't change that often. I would also like to allow the user to modify the update frequency in the future.

I've even tried increasing the policy to update after 24 hours, but it still updates every 15-45 minutes. How can I tell the widget when to update?

Arjun
  • 376
  • 3
  • 13
  • When you say "widget being updated", are you referring to the next snapshot in your timeline, or are you referring to the timeline itself being updated, and therefore a new snapshot is being shown? Also, what type, exactly, is `result`? – Yrb Jul 09 '22 at 19:27
  • I believe the timeline itself, since it only contains one entry. I want it to update based on the time I provide in the policy. `result` is the TimelineEntry which contains data I retrieve via a network call. – Arjun Jul 10 '22 at 04:12
  • 1
    So, this is assuming that you actually ONLY need to update the timeline when new data came in, you should set your policy to `.never`, and call `reloadTimelines(ofKind:)` for the widget that needs to be updated if you have new data. That is Apple's suggested way. [See this question.](https://stackoverflow.com/q/63068503/7129318) – Yrb Jul 10 '22 at 12:54
  • I need the widget to be updated when the app is in the background though. You can only call ‘reloadTimelines’ when the app is open. – Arjun Jul 10 '22 at 15:59
  • 1
    Yes, you can. Please review[Widgets Code-along, part 3: Advancing timelines](https://developer.apple.com/videos/play/wwdc2020/10036/), especially starting at 2:20. – Yrb Jul 10 '22 at 16:25
  • I see, but how can I dictate exactly when the network call should be made? I don't want it to update whenever there is new data, since new data could exist every few seconds (i.e. stock price). I just want the widget to make a network call in a specified time interval. I thought this was the purpose of the `.after` policy, but it doesn't seem to respect the date I provide. – Arjun Jul 10 '22 at 18:31
  • Those are questions WAY outside of the scope of the question. You have not provided a Minimal, Reproducible Example (MRE)](https://stackoverflow.com/help/minimal-reproducible-example) or discussed, at all, how your app works. These are questions that should have been answered for the main part of the app. – Yrb Jul 11 '22 at 14:42

0 Answers0