Debug Widget Timeline and refresh

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

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
Try running your extension target directly on the Simulator / Device after adding it to your Home Screen.
You can also attach to the process (Debug -> Attach to Process) and choose the widget extension process after running the app.
@ricob : I did run it in the simulator (I chose the extension target and ran it), but I can't see any print or enter any breakpoint, that's weird :-/
@yoshi_i : thanks, I'll try that too
@radada I have the same problem, I tried already to attach with he debugger the processes, but is not helping, still the breakpoints are not working.
anyone else find a way to debug it?
thanks
Arie
To debug, or see print info: (This works for me)

On the top left of Xcode->
Click project name, you can see a list, select widget name, run it
Click widget name, you can see a list, select project name, run it 
Xcode: Version 12.3, iPad: iPadOS 14.3 



For me getTimeline() again each time when I close the iOS Simulator App and then Re-Run the Widget Target from XCode. Breakpoints are working just fine.
Debug Widget Timeline and refresh
 
 
Q