0

I trying to build app like native Apple Notes. And I have a question\problem. In apple notes when you open already existing note again and delete all text from it - note deleting and data from core data deleting too. How can I do it? First view of app - List of notes, and second view - note. I can't understand how delete that object, what I need. For example: when my segue from list to note look like that:

-(void) prepareForSegue:(UIStoryBoardSegue *)segue sender:(id)sender{
      NoteDetailVC *destViewController = segue.destinationViewController;
      if([[segue identifier] isEqualToString:@"ShowNote"]){
            NSManagedObject *selectedNote = [self.notes objectAtIndex[[self.tableView indexPathForSelectedRow] row]];
            destViewController.selectedNoteInfo = selectedNote;
      }
} 

And in NoteDetailVC I interact with data some like that:

if (selectedNoteInfo){
 // bla bla bla code
}

On create I use setValue: command and else. I understand how dismiss controller without saving data before I set new value. But don't understand how delete already existing object from core data. How check what index I need and etc? Help please! :-)

And sorry for my English again :)

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Vladislav Kovalyov
  • 763
  • 10
  • 24
  • You might want to have a look at how to use [key-value in coredata](http://stackoverflow.com/questions/1834125/nsmanagedobject-subclasses-and-setvaluesforkeyswithdictionary) and on how to [delete](http://stackoverflow.com/questions/6524876/removing-a-specific-entry-row-from-core-data) – David Karlsson May 16 '13 at 19:30

1 Answers1

1

Here is the approach I would take, given I am understanding your question correctly.

When the user chooses to create a new note, create your NSManagedObject to represent that.

Note *newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:self.context];

When they go back to the list or press Done... In prepareForSegue:sender: check the contents of the note.

if (note.contents.length == 0) {
    [self.context deleteObject:note];
}

[self.context save:&error]
Chris Wagner
  • 20,773
  • 8
  • 74
  • 95