问题描述
我不明白当我尝试通过调用 viewContext.execute(deleteRequest) 删除所有项目时,SwiftUI 不会重绘 UI。 我看到来自 sqlite 的项目不见了。
struct CloudKitTestView: 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 {
vstack {
Button("Remove all") {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Item")
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do {
try viewContext.execute(deleteRequest)
} 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)")
}
}
List {
ForEach(items) { item in
Text("Item at \(item.timestamp!,formatter: itemFormatter)")
}
.onDelete(perform: deleteItems)
}
.toolbar {
#if os(iOS)
EditButton()
#endif
Button(action: addItem) {
Label("Add Item",systemImage: "plus")
}
}
}
}
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,\(nsError.userInfo)")
}
}
}
}
解决方法
Core Data 批量更新不会更新内存中的对象。之后您必须手动刷新。
批量操作绕过普通的 Core Data 操作,直接在底层 SQLite 数据库(或任何支持持久存储的数据库)上操作。他们这样做是为了提高速度,但这意味着他们也不会触发您使用正常获取请求获得的所有内容。
您需要执行 Apple 的 Core Data Batch Programming Guide:Implementing Batch Updates - Updating Your Application After Execution 中所示的操作
do {
let fetch: NSFetchRequest<NSFetchRequestResult> = Item.fetchRequest()
let request = NSBatchDeleteRequest(fetchRequest: fetch)
request.resultType = .resultTypeObjectIDs
let result = try viewContext.execute(request) as? NSBatchDeleteResult
let objIDArray = result?.result as? [NSManagedObjectID]
let changes = [NSDeletedObjectsKey: objIDArray]
NSManagedObjectContext.mergeChanges(fromRemoteContextSave: changes,into: [viewContext])
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError),\(nsError.userInfo)")
}