如何使用核心数据框架更新数据?

时间:2015-12-26 15:45:10

标签: ios swift core-data

我正在创建笔记应用,当我在编辑笔记内容后按“完成”按钮,而不是更新内容时,它只会创建一个新笔记并保留旧笔记而不进行更新。

我如何解决这个问题

我确切地知道问题在哪里

这是代码

@IBAction func save(sender:UIBarButtonItem) {
        let title = titleField.text
        let text = textView.text


        if (text.isEmpty){

            let alertController = UIAlertController(title: "Warning !", message: "You need to write something first", preferredStyle: .Alert)


            let okayAction = UIAlertAction(title: "OK", style: .Default) { (action) in
                print(action)
            }
            alertController.addAction(okayAction)

            self.presentViewController(alertController, animated: false) {

            }


            }else{


            if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
                note = NSEntityDescription.insertNewObjectForEntityForName("Note", inManagedObjectContext: managedObjectContext) as! NoteData
                note.title = title!
                note.text = text!




                do {
                    try managedObjectContext.save()
                } catch {
                    print(error)
                    return
                }
            }

            self.navigationController?.popViewControllerAnimated(true)
        }


    }

enter image description here

2 个答案:

答案 0 :(得分:1)

您正在创建新笔记,因为您在保存功能中调用NSEntityDescription.insertNewObjectForEntityForName

您可以保留对当前正在更新的注释的引用,更新其文本,然后保存它。或者您可以使用“查找或创建”模式。这将使您能够首先根据某些条件搜索现有注释,并且如果找到符合条件的注释,则返回它或创建符合条件的新注释。虽然,只是保留对您当前正在处理的笔记的引用是一个更好的选择。

答案 1 :(得分:0)

如果笔记是新创建的并且qual为nil而不是" insert"否则做"更新"

if note == nil {


            if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
                note = NSEntityDescription.insertNewObjectForEntityForName("Note", inManagedObjectContext: managedObjectContext) as? NoteData
                note!.title = title!
                note!.text = text!




                do {
                    try managedObjectContext.save()
                } catch {
                    print(error)
                    return
                }
            }
            // Dismiss the table view controller

            self.navigationController?.popViewControllerAnimated(true)
        }else{


            let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext


            let title = titleField.text
            let text = textView.text



            let fetchRequest = NSFetchRequest()
            fetchRequest.entity = NSEntityDescription.entityForName("Note", inManagedObjectContext: managedObjectContext)
            fetchRequest.includesPropertyValues = true


            do {
                let fetchedEntities = try self.managedObjectContext.executeFetchRequest(fetchRequest) as! [NoteData]
                fetchedEntities.first?.title = title!
                fetchedEntities.first?.text = text
            } catch {
                // Do something in response to error condition
            }

            do {
                try self.managedObjectContext.save()
            } catch {
                // Do something in response to error condition
            }




            self.navigationController?.popViewControllerAnimated(true)
        }
    }