Hi,
pretty dumb question, but how can we debug a widget, especially a TimelineProvider ?
I added breakpoints, print statements, it seems that I never refresh my Widget :-/
When a user adds some content in my app, I save the data to a shared UserDefauls as follows
Here is my TimeLineProvider
I can't see what am I doing wrong :-/
I do enter in the
Thanks for your help
pretty dumb question, but how can we debug a widget, especially a TimelineProvider ?
I added breakpoints, print statements, it seems that I never refresh my Widget :-/
When a user adds some content in my app, I save the data to a shared UserDefauls as follows
Code Block func save() { let encoder = JSONEncoder() if let data = try? encoder.encode(self) { UserDefaults.group.set(data, forKey: "lastPost") UserDefaults.group.synchronize() WidgetCenter.shared.getCurrentConfigurations { (result) in guard case .success(let widgets) = result else { return } widgets.forEach { widget in WidgetCenter.shared.reloadTimelines(ofKind: widget.kind) } } } }
Here is my TimeLineProvider
Code Block struct Provider: TimelineProvider { @AppStorage("lastPost", store: UserDefaults(suiteName: "group.com.kaiman.apps")) var currentModelData: Data? var currentModel: ExtensionSharedModel? { guard let data = currentModelData else { return nil } return ExtensionSharedModel.retrieve(from: data) } func placeholder(in context: Context) -> WidgetModelEntry { print("🏵 placeholder") return WidgetModelEntry(date: Date()) } func getSnapshot(in context: Context, completion: @escaping (WidgetModelEntry) -> ()) { print("🏵 getSnapshot") let currentDate = Date() if let model = currentModel, model.date.isToday { print("🏵 add model \(model.displayName)") completion(WidgetModelEntry(date: currentDate, model: model)) } else { print("🏵 no models to add...") completion(WidgetModelEntry(date: currentDate)) } } func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) { print("🏵 getTimeline") var entries: [WidgetModelEntry] = [] // Generate a timeline consisting of five entries an hour apart, starting from the current date. let currentDate = Date() if let model = currentModel, model.date.isToday { print("🏵 add model \(model.displayName)") entries = [WidgetModelEntry(date: currentDate, model: model), WidgetModelEntry(date: currentDate.dateAtEndOf(.day), model: model)] } else { print("🏵 no models to add...") entries = [WidgetModelEntry(date: currentDate.dateAtEndOf(.day))] } let timeline = Timeline(entries: entries, policy: .atEnd) completion(timeline) } }
I can't see what am I doing wrong :-/
I do enter in the
Code Block WidgetCenter.shared.reloadTimelines(ofKind: widget.kind)
, the data is saved to the shared UD.Thanks for your help