设置 Widget 的 TimelineProvider 刷新间隔 [英] Setting the TimelineProvider refresh interval for Widget

查看:39
本文介绍了设置 Widget 的 TimelineProvider 刷新间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我的 Widget 每秒发出大约 1 个请求.我想在 1 小时内将其更改为 1 个请求.

Right now my Widget makes about 1 request per second. I want to change it to 1 request in 1 hour.

我在使用 let timeline = ... 时遇到一些错误,例如 可选类型日期?"的值必须解包为日期"类型的值 等等.

I get some errors on the line with let timeline = ... like Value of optional type 'Date?' must be unwrapped to a value of type 'Date' and some more.

任何可能出错的建议:

struct Provider: IntentTimelineProvider {
    let networkManager = NetworkManager()
    func placeholder(in context: Context) -> SimpleEntry {
        SimpleEntry(date: Date(), configuration: ConfigurationIntent(), clubname: networkManager.clubName)
    }
    
    func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
        let entry = SimpleEntry(date: Date(), configuration: configuration, clubname: networkManager.clubName)
        completion(entry)
    }
    
    func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
        var entries: [SimpleEntry] = []
        
        // Generate a timeline consisting of five entries an hour apart, starting from the current date.
        let currentDate = Date()
        for hourOffset in 0 ..< 5 {
            let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
            let entry = SimpleEntry(date: entryDate, configuration: configuration, clubname: networkManager.clubName)
            entries.append(entry)
        }
        
        //wie oft geupdatet werden soll
        
        let nextUpdate = Calendar.current.date(byAdding: .hour, value: 1, to: Date())
        
        let timeline = Timeline(entries: entries, policy: .after(nextUpdate))
        completion(timeline)
    }
}

推荐答案

你不应该依赖 TimelineReloadPolicy - 它指定了刷新时间线的最早日期,不保证它会在特定时间重新加载.

You shouldn't rely on the TimelineReloadPolicy - it specifies the earliest date when the Timeline is refreshed, it is not guaranteed that it reloads at that specific time.

根据我的观察,Widget 更有可能使用 atEnd 策略重新加载时间线.

From my observations, a Widget is more likely to reload the timeline with the atEnd policy.

指定 WidgetKit 请求新时间线的策略在时间线中的最后一个日期过去之后.

A policy that specifies that WidgetKit requests a new timeline after the last date in a timeline passes.

这是一个可能的解决方案:

Here is a possible solution:

func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
    print("getTimeline")
    let entries = [
        SimpleEntry(date: Date()),
        SimpleEntry(date: Calendar.current.date(byAdding: .minute, value: 1, to: Date())!),
    ]

    let timeline = Timeline( entries: entries, policy: .atEnd)
    completion(timeline)
}

请注意,可能会使用也可能不会使用最后一个条目.如果在第二个条目的日期之后立即刷新时间线,则不会显示第二个条目.但是,有时时间线可能会延迟重新加载 - 然后第二个条目将可见(直到时间线刷新).

Note that the last entry may or may not be used. If the timeline is refreshed immediately after the second entry's date, the second entry won't be shown. However, sometimes the timeline may be reloaded with a delay - then the second entry will be visible (until the timeline is refreshed).

这篇关于设置 Widget 的 TimelineProvider 刷新间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆