视图与@FetchRequest不反映在CloudKit数据库的变化



我在我的应用程序中使用CoreData + CloudKit,我的视图没有反映CloudKit的变化。例如,当我删除web CloudKit仪表板上的记录时,它们仍然显示在我的模拟器视图中。在Xcode中再次运行应用程序仍然会在模拟器中反映旧数据。

我能够重新创建这个问题使用默认的Xcode项目与CloudKit和CoreData框勾选和最小的添加到Persistence.swift.


这是代码

Persistence.swift


import CloudKit
import CoreData
struct PersistenceController {
static let shared = PersistenceController()
static var preview: PersistenceController = {
let result = PersistenceController(inMemory: true)
let viewContext = result.container.viewContext
for _ in 0..<10 {
let newItem = Item(context: viewContext)
newItem.timestamp = Date()
}
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error (nsError), (nsError.userInfo)")
}
return result
}()
let container: NSPersistentCloudKitContainer
init(inMemory: Bool = false) {
container = NSPersistentCloudKitContainer(name: "TestProject")
let containerIdentifier = "[MY CONTAINER IDENTIFIER]"

if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}

// Default store
let defaultStoreLocation = container.persistentStoreDescriptions.first!.url!

// Setup public store
let publicStoreLocation = defaultStoreLocation.deletingLastPathComponent().appendingPathComponent("public.sqlite")
let publicStoreDescription = NSPersistentStoreDescription(url: publicStoreLocation)
publicStoreDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(
containerIdentifier: containerIdentifier)
publicStoreDescription.cloudKitContainerOptions?.databaseScope = .public

// Load persistent store descriptions
container.persistentStoreDescriptions = [publicStoreDescription]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error (error), (error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
}
}

这是默认的ContentView

ContentView.swift

import SwiftUI
import CoreData
struct ContentView: View {
@Environment(.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: Item.timestamp, ascending: true)],
animation: .default)
private var items: FetchedResults<Item>
var body: some View {
NavigationView {
List {
ForEach(items) { item in
NavigationLink {
Text("Item at (item.timestamp!, formatter: itemFormatter)")
} label: {
Text(item.timestamp!, formatter: itemFormatter)
}
}
.onDelete(perform: deleteItems)
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
}
ToolbarItem {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
Text("Select an item")
}
}
private func addItem() {
withAnimation {
let newItem = Item(context: viewContext)
newItem.timestamp = Date()
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error (nsError), (nsError.userInfo)")
}
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { items[$0] }.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error (nsError), (nsError.userInfo)")
}
}
}
}
private let itemFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .medium
return formatter
}()
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environment(.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}

这是默认的App

TestProjectApp.swift

import SwiftUI
@main
struct TestProjectApp: App {
let persistenceController = PersistenceController.shared
var body: some Scene {
WindowGroup {
ContentView()
.environment(.managedObjectContext, persistenceController.container.viewContext)
}
}
}

看看这个https://developer.apple.com/videos/play/wwdc2020/10650/在14:00左右删除公共数据库中的项目。

例如,您必须设置一个标志isstrashed,然后根据该标志过滤条目,而不是为公共数据库删除它们。

最新更新