|
I have an app that uses Core Data with CloudKit. Changes are synced between devices. The main target has Background Modes capability with checked Remote notifications. Main target and widget target both have the same App Group, and both have iCloud capability with Services set to CloudKit and same container in Containers checked. My goal is to display actual Core Data entries in SwiftUI WidgetKit view. My widget target file:
But I have a problem: let's say I have 3 At the start widget view shows 3 records as expected in preview (UI for adding a widget). But after I add a widget to the home screen, it does not show Or: At the start widget view shows 3 records as expected, but after a minute or so, if I delete or add Is there any way to ensure my widget shows correct fetch request results? Thanks. |
|
Hi, If I understand your question correctly you want Widgets to update not just on the timelineProvider schedule but also when you make a relevant change in your app. I add this code in my host app whenever I save something that affects the Widget.
Don't forget to import WidgetKit. This reloads the timelines and gets you what you want. If you are looking for something that will trigger an update to the widget on your iPhone when you update the core data on your mac then you will need to register for remote updates with the NSPersistentStoreRemoteChangeNotificationPostOptionKey in your store description to recieve those updates on each device. You can then call the reloadAllTimelines() from there as well. Hope this helps. Let me know if I missed the question. I have the small and medium widgets working well with CD but have not figured out how I want to use the large yet. |
|
Hello @lyncht22, thank you for your reply. I changed my
But now I get a fatal error, when I run widget target on device. Error:
As far as I understand, I need to enable Remote notifications checkbox in Background Modes capability, but I don't know how to enable Background Modes capability in Widget target... Can it be done? 🧐 |
|
My goal is to display updated rows from Core Data. As far I understand — widget views don't observe anything. They're just provided with |
|
Hi @gh-rusinov, I don't think you need to re-init the container. I understood that you had this working with Core Data in the widget already. I am using CoreData from a swift package so I import into both my app and the widget. Also, I am using the views from the package as well as opposed to building them right there in the widget file. Ex Code
The updates are called and registered from the same stack. I would recommend: 1) Go back to the container setting you had where it was working in the widget as well. 2) From the main app call WidgetCenter.shared.reloadAllTimelines() whenever you save something in order to make sure the widget updates itself. Oh, it might help to confirm in your widget that you have AppGroup capability with your group container checked. // Adding more comments Just looking at your TimelineEntry code. You should update that to include your NSManagedObject. That will force you to update a few more items. I think that will help.
Thanks |
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.