Vladislav Kovalyov
Vladislav Kovalyov

Reputation: 803

Delete existing data from another view

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

Upvotes: 0

Views: 63

Answers (1)

Chris Wagner
Chris Wagner

Reputation: 20993

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]

Upvotes: 1

Related Questions