1

I know when accessing Core Data across targets I need to use an App Group and common persistentContainer configuration. I'm able to read without issue, and I can create as well.

However, where I'm stuck is when the other target creates and saves an item, the primary app's NSFetchedResultsController doesn't see the change. If I restart the app the data then shows up, so I know it's getting stored properly. I also tried to catch the NSManagedObjectContextDidSave notification in the primary target, but that doesn't seem to do it either.

NotificationCenter.default.addObserver(forName: Notification.Name.NSManagedObjectContextDidSave,
                                       object: nil,
                                       queue: nil) { notification in
                                        print("Got called")
                                        self.managedObjectContext.mergeChanges(fromContextDidSave: notification)

How do I let the NSFetchedResultsController in the primary target know something got saved from another target?

I'm setting up the persistentContainer like so:

lazy private var persistentContainer: NSPersistentContainer = {
  let groupName = "group.com.contoso.CoolCalendar"
  let url = FileManager.default
    .containerURL(forSecurityApplicationGroupIdentifier: groupName)!
    .appendingPathComponent("CoolCalendar.sqlite")

  let container = NSPersistentContainer(name: "CoolCalendar")

  container.persistentStoreDescriptions = [
    NSPersistentStoreDescription(url: url)
  ]

  container.loadPersistentStores(completionHandler: { _, error in
    if let error = error as NSError? {
      fatalError("Unresolved error \(error), \(error.userInfo)")
    }
  })

  return container
}()
Gargoyle
  • 9,590
  • 16
  • 80
  • 145
  • 1
    [This](https://stackoverflow.com/questions/7830778/nsmanagedobjectcontext-with-different-processes) and [this code](https://github.com/maximbilan/iOS-Shared-CoreData-Storage-for-App-Groups) are talking about how to solve it. – Fabian Aug 11 '18 at 19:21
  • @Purpose I had seen that one and tried it, but the notification doesn't seem to ever arrive in the main target. I must still be missing something. – Gargoyle Aug 11 '18 at 19:23
  • That relies on a notification crossing the target processes, but that doesn't seem to actually work. I'm really confused. – Gargoyle Aug 11 '18 at 19:28
  • [Here](https://stackoverflow.com/questions/37581441/nsmanagedobjectcontextdidsavenotification-useless) it says that persistent store changes are automatically transmitted to root contexts. Dunno whether that works over processes though. – Fabian Aug 11 '18 at 19:32
  • [This](https://stackoverflow.com/questions/1815518/how-do-i-share-a-core-data-store-between-processes-using-nsdistributednotificati) talks about a stalenessInterval and possibly reset. Staleness might only be relevant if the cache is enabled though. – Fabian Aug 11 '18 at 19:38
  • [This](https://stackoverflow.com/questions/1691811/is-there-a-way-of-sharing-a-core-data-store-between-processes) says that you can use NSDistributedNotificationCenter to tell the other process to merge changes from disk. – Fabian Aug 11 '18 at 19:51

0 Answers0