首页 > 解决方案 > SwiftUI (iOS 14) 应用遇到 NSMergeConflict

问题描述

虽然 StackOverflow 中有几个合并冲突问题,但似乎都是 4 年多以前的问题,并且由于代码/想法,我无法使用 CloudKit 集成到新的基础 SwiftUI 应用程序中。它不会每次都发生,但可能有三分之一的记录让我陷入了一个致命的错误:

NSManagedObjectContext+Extension.swift-16: Unresolved error Error Domain=NSCocoaErrorDomain Code=133020 "Could not merge changes." UserInfo={conflictList=(
    "NSMergeConflict (0x28359e700) for NSManagedObject (0x280ca2f30) with objectID '0x8e8643f3d377df5d <x-coredata://F0AEE26E-FCF0-464B-A886-6A781D2D28D9/Journal/p5>' with oldVersion = 2 and newVersion = 4 and old object snapshot = {\n    dateCreated = \"2021-07-27 00:29:32 +0000\";\n    id = \"4258E1DA-9EE3-487B-87F4-41023D4857E8\";\n    name = \"Test Journal\";\n} and new cached row = {\n    dateCreated = \"2021-07-27 00:29:32 +0000\";\n    id = \"4258E1DA-9EE3-487B-87F4-41023D4857E8\";\n    name = \"Test Journal\";\n}"
), NSExceptionOmitCallstacks=true}, ["NSExceptionOmitCallstacks": 1, "conflictList": 

我的 Persistence.swift 文件是库存。

内容视图

struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(fetchRequest: Journal.allRecordsByNameFetchRequest)
private var journals: FetchedResults<Journal>
@State var showEditJournalSheetView = false
    
    var body: some View {
        NavigationView {
            ZStack(alignment: .bottomTrailing) {
                List {
                    ForEach(journals, id: \.id) { journal in
                        NavigationLink(destination: makeJournalDetailView(from: journal)) {
                            makeJournalRow(from: journal)
                        }
                    }
                }
            }
            .sheet(isPresented: $showEditJournalSheetView) {
                EditJournalView(journal: nil, showEditJournalSheetView: $showEditJournalSheetView)
                    .environment(\.managedObjectContext, viewContext)
            }

编辑期刊视图:

struct EditJournalView: View {
    @Environment(\.managedObjectContext) private var viewContext
    let journal: Journal?

.......

.toolbar {
                ToolbarItem(placement: .primaryAction) {
                    Button("Save") {
                        if isFormValid() {
                            showFormErrorMessage = false
                            if journal != nil {
                                saveJournalChanges() //TODO: Maybe add closure so doesn't close out too early
                            } else {
                                createNewJournal()
                            }
                            self.showEditJournalSheetView = false
                        } else {
                            showFormErrorMessage = true
                        }
                    }
                }


.......

private func createNewJournal() {
        Journal.create(name: name, context: viewContext)
    }

最后,

日记+扩展.swift

extension Journal {
    static func create(name: String?, context: NSManagedObjectContext) {
        let newJournal = Journal(context: context)
        newJournal.id = UUID()
        newJournal.name = name?.isEmpty == false ? name : "Untitled"
        newJournal.dateCreated = Date()

        context.saveContext()
    }

任何帮助将不胜感激!

标签: core-dataswiftui

解决方案


推荐阅读