SwiftUI:删除所有核心数据实体条目后,列表不会自动更新 [英] SwiftUI: List does not update automatically after deleting all Core Data Entity entries

查看:119
本文介绍了SwiftUI:删除所有核心数据实体条目后,列表不会自动更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道SwiftUI使用状态驱动的渲染。所以我以为,当我删除核心数据实体条目时,带有核心数据元素的列表会立即刷新。
我使用以下代码,成功清除了我的实体:

I know SwiftUI uses state-driven rendering. So I was assuming, when I delete Core Data Entity entries, that my List with Core Data elements gets refreshed immediately. I use this code, which gets my Entity cleaned succesfully:

func deleteAll()
{
    let fetchRequest: NSFetchRequest<NSFetchRequestResult> = ToDoItem.fetchRequest()
    let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)

    let persistentContainer = (UIApplication.shared.delegate as! AppDelegate).persistentContainer

    do {
        try persistentContainer.viewContext.execute(deleteRequest)
    } catch let error as NSError {
        print(error)
    }
}

要使视图中的列表视觉上为空,我之后必须离开视图(例如,使用self.presentationMode.wrappedValue。 dismiss()),然后再次将其打开。好像这些值仍存储在内存中的某个地方。
这当然不是用户友好的,我敢肯定,我只是监督着可以立即刷新列表的内容。
也许有人可以帮忙。

To get the List in my View visually empty I have to leave the View afterwards (for example with " self.presentationMode.wrappedValue.dismiss()") and open it again. As if the values are still stored somewhere in the memory or something. This is of course not user-friendly and I am sure I just oversee something that refreshes the List immediately. Maybe someone can help.

推荐答案

原因是执行(如下文详细描述-注意第一句话)不会影响托管对象的上下文,因此所有获取的对象都保留在上下文中,UI表示上下文真正呈现的内容。

The reason is that execute (as described in details below - pay attention on first sentence) does not affect managed objects context, so all fetched objects remains in context and UI represents what is really presented by context.

因此,通常,在执行此批量操作后,您需要通知该代码(此处未提供)强制同步并重新获取所有内容。

So in general, after this bulk operation you need to inform back to that code (not provided here) force sync and refetch everything.

API接口声明


// Method to pass a request to the store without affecting the contents of the managed object context.
// Will return an NSPersistentStoreResult which may contain additional information about the result of the action
// (ie a batch update result may contain the object IDs of the objects that were modified during the update).
// A request may succeed in some stores and fail in others. In this case, the error will contain information
// about each individual store failure.
// Will always reject NSSaveChangesRequests.
@available(iOS 8.0, *)
open func execute(_ request: NSPersistentStoreRequest) throws -> NSPersistentStoreResult


例如,可能是以下方法(从头开始)

For example it might be the following approach (scratchy)

// somewhere in View declaration
@State private var refreshingID = UUID()

...
// somewhere in presenting fetch results
ForEach(fetchedResults) { item in
    ...
}.id(refreshingID) // < unique id of fetched results

...

// somewhere in bulk delete 
try context.save() // < better to save everything pending
try context.execute(deleteRequest)
context.reset() // < reset context
self.refreshingID = UUID() // < force refresh

这篇关于SwiftUI:删除所有核心数据实体条目后,列表不会自动更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆