WidgetKit@StateObject未更新视图



我很难理解如何使SwiftUI数据模型示例与我的Widget一起工作。它在我的测试应用程序中运行得很好,我会立即观察到变化。当我尝试使用Widget时,我可以看到控制台中打印的数据,但WidgetKit中的View没有发生任何更改。我使用的是ObservableObject类和@Published变量。我尝试过使用@StateObject@ObservedObject@EnvironmentObject,结果都是一样的。总是导致今天没有比赛。我的数据模型与Combine相关,不确定这是否与为什么我在WidgetKitView中看不到数据更改有关。

SwiftUI应用程序示例工作

struct ContentView: View {
@ObservedObject var data = CombineData()
@State private var showSortSheet: Bool = false
@State private var nhlTeams: TeamOrder = .NewYorkIslanders

var body: some View {
NavigationView {
VStack(alignment: .leading) {
if data.schedule?.dates.first != nil {
Text("Game today")
} else {
Text("No game today")
}
}
.listStyle(PlainListStyle())
.navigationBarTitle(Text(""), displayMode: .inline)
.navigationBarItems(leading: HStack {
Button(action: {
self.showSortSheet.toggle()
}) {
HStack {
Image(systemName: "arrow.up.arrow.down")
}
}
})
.actionSheet(isPresented: $showSortSheet) {
ActionSheet(title: Text("Teams:"), buttons: [TeamOrder.NewYorkIslanders, TeamOrder.MontrealCanadiens].map { method in
ActionSheet.Button.default(Text("(method.rawValue)")) {
self.nhlTeams = method
data.fetchSchedule(self.nhlTeams.rawValue)
print("(self.nhlTeams.rawValue)")
}
})
}
.onAppear {
data.fetchSchedule(self.nhlTeams.rawValue)
}
}
}
}

示例小工具@StateObject(无数据更改(

struct ExampleEntryView : View {
var entry: Provider.Entry
@Environment(.widgetFamily) var widgetFamily
@StateObject var model = CombineData()
@ObservedObject var model = CombineData()
/*@EnvironmentObject private var model: CombineData*/
var body: some View {
VStack(alignment: .leading) {
if model.schedule?.dates.first != nil {
Text("Game today")
} else {
Text("No game today")
}
}
.onAppear {
model.fetchSchedule(entry.configuration.teams.rawValue) {
WidgetCenter.shared.reloadTimelines(ofKind: "NHL_Widget")
}
}
}
}

示例小工具2 TimelineEntry(无数据更改(

struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: ConfigurationIntent
let model: CombineData
}
struct Provider: IntentTimelineProvider {

func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [SimpleEntry] = []
let currentDate = Date()
let model = CombineData()
let entryDate = Calendar.current.date(byAdding: .minute, value: 1, to: currentDate)!
let entry = SimpleEntry(date: entryDate, configuration: configuration, model: model)
entries.append(entry)
let timeline = Timeline(entries: entries, policy: .atEnd)
model.fetchSchedule(entry.configuration.teams.rawValue) {
completion(timeline)
}
}
}
struct ExampleEntryView : View {
var entry: Provider.Entry
@Environment(.widgetFamily) var widgetFamily
var body: some View {
VStack(alignment: .leading) {
if entry.model.schedule?.dates.first != nil {
Text("Game today")
} else {
Text("No game today")
}
}
}
}

小部件是静态的,您在getTimeline上提供的视图将按原样显示,不会更新,因此onAppear上对fetchSchedule的调用甚至不会执行。一旦在getTimeline中执行完处理程序,就不会再执行任何代码。

要在小部件上显示数据,您需要在getTimeline中获取数据,创建一个从一开始就显示数据的视图,并在完成处理程序中返回该条目。

在上一个示例中,我建议您在model.fetchSchedule的完成处理程序中创建条目,并将schedule对象传递给视图。

相关内容

  • 没有找到相关文章

最新更新