Accessing Core Data from both container app and extension

后端 未结 1 1143
失恋的感觉
失恋的感觉 2020-12-14 04:03

I\'m developing app and share extension and trying to use core data. But when I\'m inserting items in the extension those items only visible in extension but not from conta

相关标签:
1条回答
  • 2020-12-14 04:57
     lazy var persistentContainer: NSPersistentContainer = {
        /*
         The persistent container for the application. This implementation
         creates and returns a container, having loaded the store for the
         application to it. This property is optional since there are legitimate
         error conditions that could cause the creation of the store to fail.
         */
        let container = NSPersistentContainer(name: "xx")
    
        let appName: String = "xx"
        var persistentStoreDescriptions: NSPersistentStoreDescription
    
        let storeUrl =  FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.xx.xx.container")!.appendingPathComponent("xx.sqlite")
    
    
        let description = NSPersistentStoreDescription()
        description.shouldInferMappingModelAutomatically = true
        description.shouldMigrateStoreAutomatically = true
        description.url = storeUrl
    
        container.persistentStoreDescriptions = [NSPersistentStoreDescription(url:  FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.xxx.xx.container")!.appendingPathComponent("xx.sqlite"))]
    
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()
    
    0 讨论(0)
提交回复
热议问题