gpt4 book ai didi

ios - 从容器应用程序和扩展程序访问核心数据

转载 作者:技术小花猫 更新时间:2023-10-29 11:13:41 26 4
gpt4 key购买 nike

我正在开发应用程序和共享扩展程序并尝试使用核心数据。但是,当我在扩展中插入项目时,这些项目仅在扩展中可见,但在容器应用程序中不可见(例如,我从应用程序执行 NSFetchRequest 并获得零项目,但在应用程序中我得到 >0)。我正在使用以下代码获取持久容器:

lazy var persistentContainer: NSPersistentContainer = {

let container = NSPersistentContainer(name: "appname")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error {

fatalError("Unresolved error \(error)")
}
})
return container
}()

此外,appname.xcdatamodeld 的目标成员船也会针对应用程序和扩展程序进行检查。如何为容器应用和扩展正确共享核心数据?

最佳答案

 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
}()

关于ios - 从容器应用程序和扩展程序访问核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41684256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com