我玩过新的 iOS 14 小部件,除了自动刷新外,似乎工作得差不多了。我的目标是让我的小部件在每个午夜刷新一次(因此,每天只刷新一次,当然偶尔会在我的应用程序中发生某些事情时)。在 XCode 中,我将其设置为每分钟刷新一次,并且似乎非常随机地工作 - 所以它可能在我测试的 10 分钟内刷新一次,但我想,没关系,因为我读到刷新是由 iOS 处理的,所以它迟早会出现,但终会到来。
现在,我已经发布了我的应用程序的这个版本,今天早上(早上 8 点左右,午夜过后)我检查了我的小部件,但今天没有更新。
我的问题,我做错了吗?还是它还没有那么稳定?
我的时间线仅包含一项date: now
并将刷新策略设置为.after(nextMidnight)
. 我也尝试使用 refresh policy .atEnd
,但是由于我在时间轴中只有一个设置为现在的项目,它不会尝试连续刷新吗?我想,.after
是更好的方法。
如果它是相关的,它是StaticConfiguration
而不是IntentConfiguration
(老实说,我仍然需要深入研究这里的区别是什么......)。
代码片段:
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
let now = Date()
let calendar = Calendar.current
var dateComponents = DateComponents()
dateComponents.year = calendar.component(.year, from: now)
dateComponents.month = calendar.component(.month, from: now)
dateComponents.day = calendar.component(.day, from: now) + 1
dateComponents.hour = 0
dateComponents.minute = 0
dateComponents.second = 0
let nextUpdate = calendar.date(from: dateComponents)
completion(Timeline(entries: [SimpleEntry(date: now)], policy: .after(nextUpdate!)))
}