【问题标题】:Updating values in Core Data leads to duplicate entries in Swift更新 Core Data 中的值会导致 Swift 中的重复条目
【发布时间】:2019-05-20 20:59:07
【问题描述】:

我正在尝试从我的 iPad 应用程序的核心数据中更新值。不幸的是,每次我更新数据时,都会创建一个重复的条目。

我编写了下面的代码,该代码采用包含值的参数并尝试更新。但不幸的是,它会创建一个重复条目,而不是更新现有条目。

        let appDelegate = UIApplication.shared.delegate as! AppDelegate

        let context = appDelegate.persistentContainer.viewContext

        let entity = NSEntityDescription.entity(forEntityName: "Project", in: context)
        let newProject = NSManagedObject(entity: entity!, insertInto: context)

        let projectID = 1

        newProject.setValue(projectID, forKey: "projectID")
        newProject.setValue(projectName, forKey: "projectName")
        newProject.setValue(projectFinalDueDate, forKey: "projectFinalDueDate")
        newProject.setValue(ProjectNotes, forKey: "projectNotes")
        newProject.setValue(projectPriority, forKey: "projectPriority")
        newProject.setValue(projectAddedDate, forKey: "projectAddedDate")

        do {
            try context.save()
            showMessage(message: "Successfully update project", messageType: "success")
        } catch {
            showMessage(message: "Failed to update project", messageType: "error")
        }

如果我使用的关键字有错误,我很抱歉,请注意我是Core Data的新手,我几天前开始使用它。

如果有人可以帮我解决这个问题,不胜感激

Please find the screen recording of the issue

【问题讨论】:

    标签: swift core-data editing


    【解决方案1】:

    你在这里创建一个新对象

    let newProject = NSManagedObject(entity: entity!, insertInto: context)
    

    所以它会添加一个新对象,你需要使用谓词并根据一些主键更新检索到的值,然后保存上下文,如

        //As we know that container is set up in the AppDelegates so we need to refer that container.
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
        
        //We need to create a context from this container
        let managedContext = appDelegate.persistentContainer.viewContext
        
        let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "Project")
        fetchRequest.predicate = NSPredicate(format: "projectID  = %d",1)
        do
        {
            let test = try managedContext.fetch(fetchRequest)
    
             guard let newProject = test.first else { return }
                // update here 
               
               try managedContext.save()                
            }
        catch
        {
            print(error)
        }   
    }
    

    【讨论】:

    • 哦,所以如果我使用 projectID 作为主键应该可以吗?
    • 非常感谢,请问"%d", 1是做什么的?
    • 它应该使用 projectID = 1 检索对象,以便您可以更新它,请参阅此处以获取完整的 CRUD 演示 github.com/AnkurVekariya/CoreDataSwiftDemo
    • 哦,好吧,如果我想在根据添加的日期进行排序后动态获取projectID,谓词会怎样?
    • 我尝试了您在上面键入的代码,当我尝试使用newProject 设置值时,我收到以下错误Value of type 'Any' has no member 'setValue'。如果我尝试通过managedContext 设置值,我会收到以下异常Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[&lt;NSManagedObjectContext 0x6000005211d0&gt; setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key projectName.'
    猜你喜欢
    • 2013-08-18
    • 2018-04-05
    • 2019-04-17
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    相关资源
    最近更新 更多