微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

viewContext.execute(deleteRequest) 不会调用重绘 SwiftUI 列表

如何解决viewContext.execute(deleteRequest) 不会调用重绘 SwiftUI 列表

我不明白当我尝试通过调用 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 中所示的操作

Original answer

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。