Synchronize CoreData Context of iOS Widget and containing App

444 views Asked by At

I'm currently writing a today extension for one of my applications. Inside the extension the user can set bookmarks for articles. I have a ViewController in the app which displays all of the bookmarks and is hooked up to a NSFetchedResultsController.

I want to achieve that if a user adds a bookmark via the widget it automatically pops up in the application. I tried [self.managedObjectContext reset] and the NSManagedObjectContextDidChangeNotification - both without luck. When i completely exit the app and reopen it again, the changes done by the widget are visibile but not if the app still runs in the background.

Any ideas?

1

There are 1 answers

1
Andrea On

To share data between an app and its extension you need to create an AppGroup. It's not difficult but it takes different steps:

  1. Create the app group in the apple developer portal
  2. In xcode under target->capabilities enable app groups and do the same for the extension
  3. now you need to put the database in the shared bundle, if this is your first release you can change -applicationDocumentsDirectory method of core data boiler plate code to point to a shared container

Or you can share NSUserDefaults to save transitionary data between app and extension. Here a sample project and here a tutorial.