Member-only story

Fetch Data From CoreData for iOS Widget

Grace Huang
3 min readJun 30, 2022

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 -> +

Enter your group container name here. For example, for my project, it is “group.com.deelightfullabs.TallyCoin”

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.

Modify the highlighted lines in the screenshot

Note that, you should also make sure the Persistence Swift file is accessible for the widget target. Otherwise, in…

--

--

Grace Huang
Grace Huang

Written by Grace Huang

I write about startups, entrepreneurship, investing, software, hardware and manufacturing.

No responses yet