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
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
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 WidgetCenter.shared.reloadTimelines(ofKind: widget.kind)
, the data is saved to the shared UD.
Thanks for your help
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hi all,
I've got a pretty simple app (basically a Hootsuite like and light).
I'd was hoping to create a widget that show the last post if it is today, or an view that encourages the user to create a new post.
To that purpose, I create a Codable model that I store in the shared UserDefaults (suiteName) and when I do so, I call the fefrshtimeline as follows :
func save() {
let encoder = JSONEncoder()
if let data = try? encoder.encode(self) {
UserDefaults.group.set(data, forKey: "lastPost")
WidgetCenter.shared.getCurrentConfigurations { (result) in
guard case .success(let widgets) = result else { return }
widgets.forEach { widget in
WidgetCenter.shared.reloadTimelines(ofKind: widget.kind)
}
}
}
}
The thing is, I put a print in the timeLine and it's never called. I also tried to change the snapShot method, nothing works so far.
Here are both methods from the Widget :
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
print("🏵 getSnapshot")
if let model = ExtensionSharedModel.retrieve(), model.date.isToday {
print("🏵 add model \(model.displayName)")
completion(SimpleEntry(date: Date(), model: model))
} else {
print("🏵 no models to add...")
completion(SimpleEntry(date: Date()))
}
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
print("🏵 getTimeline")
var entries: [SimpleEntry] = []
if let model = ExtensionSharedModel.retrieve(), model.date.isToday {
print("🏵 add model \(model.displayName)")
entries = [SimpleEntry(date: Date(), model: model)]
} else {
print("🏵 no models to add...")
entries = [SimpleEntry(date: Date())]
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
The widget is static configuration widget.
I do enter in WidgetCenter.shared.reloadTimelines, but never in the protocol methods.
Thanks for your help :-)
Hi,I've got annotations on a MKMapView and I added a custom view inside the detailCalloutAccessoryView. This custom view performs a request and should present various amount of data depending on the request's reply. Basically, I can show 1 or 2 rows of data.Sometimes, when I touch an annotation and the result is only one row, the callout is not resized. However, if I dimiss the annotation and select it once again, it is rendered correctly.What is the "right" way to make it work? Using a intrinsicContentSize, or calling layoutIfNeeded (already tried, did not work)Thanks for your help.