1

So I currently have an service extension setup that write data to CoreData (Or I believe it does). Given that the xcdatamodelid is shared with both targets can I not write in one and read in the other?

Here is how I create the NSPersistenContainer in both targets:

lazy var persistentContainer: NSPersistentContainer = {
    let container = NSPersistentContainer(name: "commonName")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

And here is how I add an entity:

func addEntity(uid: String, status: String) {
    let context = persistentContainer.viewContext
    if let entity = NSEntityDescription.entity(forEntityName: EntityNames.alertNotificationEntity, in: context) {
let managedObj = NSManagedObject(entity: entity, insertInto: context)
        managedObj.setValue(uid, forKey: CoreDataEntityKeys.AlertNotificationEntity.uid)
        managedObj.setValue(status, forKey: CoreDataEntityKeys.AlertNotificationEntity.status)
        do {
            try context.save()
        } catch {
            print(error.localizedDescription)
        }
    }
}

I don't get any fatal when a notification comes in and the addEntity method is used.

In my appDelegate when I try to print the values out it says it's just empty.

What am I doing wrong? Is it not possible to share the CoreData between the Applications target and the service extensions target?

Edit: I should add that I have already tried to setup what is outlined in How to access CoreData model in today extension (iOS)

sbrioux
  • 11
  • 3

1 Answers1

0

Data/File between the app and extension can be shared using App group. Check Share data between app and extension

  • 1
    App groups have already been setup and UserDefaults sharing between the two targets (main app and NSE) is working but NOT CoreData though. – sbrioux Mar 18 '18 at 16:31