SwiftUI: List does not update automatically after deleting all Core Data Entity entries

前端 未结 2 896
一向
一向 2020-12-10 06:18

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

2条回答
  •  温柔的废话
    2020-12-10 06:37

    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 interface declaration

    // 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
    

提交回复
热议问题