How does save method works for NSManagedObject context


#1

This code saves changes

[code]//New

  • (BOOL)saveChanges
    {
    NSError *error;
    BOOL successful = [self.context save:&error];
    if (!successful) {
    NSLog(@“Error saving: %@”, [error localizedDescription]);
    }

    return successful;

}
[/code]

But we did not specify what to save, how does the method know what to save?


#2

Whatever state the context has at the time of the save operation is what gets saved.

The context is like a scratch pad. It’s blank immediately after its creation. The context keeps track of anything you insert, fetch, modify (attributes, relationships etc.), or delete. When you save the context, the current state of the context is committed to desk. If the context has no change then nothing is saved.