1

大家早上好,

我正在使用 API 开发一个小部件,该 API 根据小部件的大小发送不同大小的数组(小:3 个新闻,中:6 个新闻,大:9 个新闻)。

我的印象是@Environment(\.widgetFamily) var familyTimelineProvider的时间线函数(我调用API的地方)之间存在问题。

实际上,在这个函数中,环境变量总是等于 systemMedium 大小,尽管小部件的“真实”大小。

你身边是否也有同样的问题?这是来自 Apple 的已知错误吗?我该如何解决这个错误?

谢谢你的帮助 :]

4

1 回答 1

1

Without seeing your code, my best guess is that you're not accessing the family property of the TimelineProviderContext passed into the TimelineProvider.

Your TimelineProvider should look something like:

struct MyProvider: TimelineProvider {
    func snapshot(with context: Context, completion: @escaping (Entry) -> ()) {
        fetchNewsArticles(for: context.family) { articles in
              // ...
              completion(...)
        }
    }
}

func fetchNewsArticles(for widgetFamily: WidgetFamily, completion: @escaping ... ) 
{
     switch family {
         case .systemSmall:
         // ...
}

Apple Docs - TimelineProvider

Apple Docs - TimelineProviderContext

于 2020-08-04T15:02:13.830 回答