O user
O user

Reputation: 97

how to update the data using core data framework?

I'm creating note app, when I press "done" button after I edit the contents of note, instead of updating the content, it just creates a new note and leave the old note without updating.

how I can fix this problem

I know exactly where the problem is

here's the code

@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

Upvotes: 1

Views: 69

Answers (2)

O user
O user

Reputation: 97

if the note is newly created and qual to nil than do "insert" else do "update"

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)
        }
    }

Upvotes: 0

Jonathan
Jonathan

Reputation: 2383

You're creating a new note because you're calling NSEntityDescription.insertNewObjectForEntityForName in the save function.

You could just keep a reference to the note that you're currently updating, update its text, and then save it. Or you could use the Find-Or-Create pattern. This will enable to you first search for the existing note based on some criteria and if a note matching the criteria is found either return it or create a new note that matches the criteria. Although, just keeping a reference to the note you're currently working on is a much better option.

Upvotes: 1

Related Questions