核心数据 CloudKit - 完成 onMove 排序后列表未更新

Core Data CloudKit - List not updating after done onMove sorting

我有一个核心数据 + CouldKit 应用程序,其中通过 onMove 修饰符所做的任何更改(排序顺序)在点击完成按钮后不会直接显示。如果我离开视图再回来,变化是可见的。我尝试在 onMove 函数期间获取新的更新项目,但这会产生奇怪的行为。

我有以下 PersistenceController:

static let shared = PersistenceController()
    
let container: NSPersistentCloudKitContainer
    
var context: NSManagedObjectContext {
    return container.viewContext
}
    
init(inMemory: Bool = false) {
        
    container = NSPersistentCloudKitContainer(name: "MonnyModel")
        
    if inMemory {
        container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
    }
        
    container.loadPersistentStores { storeDescription, error in
        if let error = error as NSError? {
            // Replace this implementation with code to handle the error appropriately.
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    }
        
    context.automaticallyMergesChangesFromParent = true
    context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
        
    try? context.setQueryGenerationFrom(.current)
}
...

我的视图模型:

@Published var lists: [Listing] = []
    
func fetchAllLists() {
    lists = PersistenceController.shared.fetchLists()
}
    
...
    
func move(from source: IndexSet, to destination: Int) {
        
    // Make an array of items from fetched results
    var revisedLists: [Listing] = lists.map{[=12=]}
        
    // Change the order of the items in the array
    revisedLists.move(fromOffsets: source, toOffset: destination)
        
    // Update the sortOrder attribute in revisedLists to persist the new order. This is done in reverse order to minimize changes to the indices.
    for reverseIndex in stride(from: revisedLists.count - 1, through: 0, by: -1) {
        revisedLists[reverseIndex].sortOrder = Int16(reverseIndex + 1)
    }

    // I've tried fetching it here which causes strange behaviour

    PersistenceController.shared.save()
}

我试过在移动功能中再次获取,但没有成功。这样做将使列表上的 editMode 保持活动状态,但每行上的所有操作都会消失,我唯一能做的就是点击完成按钮。我也在这里尝试过 .objectWillChange.send() 但同样的奇怪行为正在发生。

这是我视图中的列表:

struct MyListView: View {
    
    @Environment(\.managedObjectContext) private var viewContext
    
    @StateObject var viewModel: ListViewModel
    
    var body: some View {
        List {
            ForEach(viewModel.lists.sorted(by: { [=13=].sortOrder < .sortOrder })) { list in
                ListCellView(viewModel: viewModel, list: list)
            }
            .onMove(perform: viewModel.move)
            .onDelete(perform: { indexSet in
                indexSet.forEach { index in
                    viewModel.deleteList(list: viewModel.lists[index])
                    viewModel.fetchAllLists()
                }
            })
        }
        .onAppear {
            viewModel.fetchAllLists()
        }
        .refreshable {
            viewModel.fetchAllLists()
        }
    }
}

我已经通过添加

解决了我的问题

.environment(\.editMode, self.$isEditMode)

Editbutton() 所在视图中的我的列表。

有了这个变量@State var isEditMode: EditMode = .inactive