Member-only story
Fetch Data From CoreData for iOS Widget
I did a lot of googling and finally figured out how to get iOS Widget to fetch the CoreData that is also shared with the main app. I hope my finding could be useful for anyone who is googling for a solution. 😃
Here are the steps -
1. Create an App Group
Go to your project in Xcode -> Signing & Capabilities -> App Groups -> +
Then, create a App Group helper in the main app directory to define the container URL, which we will be using later.
2. Update the main app’s CoreData Persistence
Since we have created an AppGroup, when we create a new PersistenceController, we will update the store description with the AppGroup URL.
This means, you are creating Core Data under the App Group, rather than the main app.
Note that, you should also make sure the Persistence Swift file is accessible for the widget target. Otherwise, in…