我有一个SwiftUI应用程序,带有一个意图扩展目标。 我还有一个位于共享应用组中的核心数据存储。
使用Shortcuts应用程序中的
我认为我需要一种方法让我的应用程序知道它已在不同的上下文中更新,但我不确定如何做到这一点?
我尝试在应用程序委托中设置
我对Core Data很陌生,非常困惑,因此任何指向正确方向的指针都将是极好的,谢谢!
以下是我当前代码的精简版本: 应用委托
使用Shortcuts应用程序中的
newBackgroundContext()
,意图扩展可以成功地将数据写入存储。 我的SwiftUI应用程序可以在viewContext
中从应用组读取数据,但只有当我强制退出应用程序并重新打开它时才能读取。我认为我需要一种方法让我的应用程序知道它已在不同的上下文中更新,但我不确定如何做到这一点?
我尝试在应用程序委托中设置
context.automaticallyMergesChangesFromParent = true
,并将context.refreshAllObjects()
添加到SwiftUI视图中的onAppear
,但似乎没有任何区别。
mergeChanges(fromContextDidSave:)似乎很有前途,但我不确定如何获取通知。我对Core Data很陌生,非常困惑,因此任何指向正确方向的指针都将是极好的,谢谢!
以下是我当前代码的精简版本: 应用委托
lazy var persistentContainer: NSCustomPersistentContainer = {
let container = NSCustomPersistentContainer(name: "exampleContainerName")
container.loadPersistentStores { description, error in
if let error = error {
print("error loading persistent core data store")
}
}
return container
}()
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
print("error saving core data context")
}
}
}
class NSCustomPersistentContainer: NSPersistentContainer {
override open class func defaultDirectoryURL() -> URL {
var storeURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "exampleAppGroupName")
storeURL = storeURL?.appendingPathComponent("exampleContainerName")
return storeURL!
}
}
场景代理
guard let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext else {
fatalError("Unable to read managed object context.")
}
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: ExampleView.environment(\.managedObjectContext, context))
self.window = window
window.makeKeyAndVisible()
}
Swift UI 视图
import SwiftUI
import CoreData
struct ExampleView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(
entity: ExampleEntityName.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: \ExampleEntityName.date, ascending: false),
]
) var items: FetchedResults<ExampleEntityName>
var body: some View {
VStack {
List(items, id: \.self) { item in
Text("\(item.name)")
}
}
}
}
意图扩展
class NSCustomPersistentContainer: NSPersistentContainer {
override open class func defaultDirectoryURL() -> URL {
var storeURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "exampleContainerName")
storeURL = storeURL?.appendingPathComponent("exampleAppGroupName")
return storeURL!
}
}
let persistentContainer: NSCustomPersistentContainer = {
let container = NSCustomPersistentContainer(name: "exampleContainerName")
container.loadPersistentStores { description, error in
if let error = error {
print("error loading persistent core data store: \(error)")
}
}
return container
}()
let context = persistentContainer.newBackgroundContext()
let entity = NSEntityDescription.entity(forEntityName: "ExampleEntityName", in: context)
let newEntry = NSManagedObject(entity: entity!, insertInto: context)
newEntry.setValue(Date(), forKey: "date")
newEntry.setValue("Hello World", forKey: "name")
do {
try context.save()
} catch {
print("Failed saving")
}