LuckyLuke
LuckyLuke

Reputation: 49047

Updating Core Data entity

How do I update Core Data entities, what actions are needed? I have a Storewith one-to-many relationship to Product. When I do a change to the products, remove one or add, do I need to do anything then such as invoking some methods? Or do I just edit products and leave them alone and then they are saved to persistent store when the user quits the application? Is that the normal way?

Upvotes: 0

Views: 951

Answers (1)

Pfitz
Pfitz

Reputation: 7344

You have to save them to be persistant - see Managed Object Context in the Core Data Programming Guide from Apple:

Unless you actually save those changes, however, the persistent store remains unaltered.

so here is the code

NSError *error = nil;
BOOL savedSuccessfully = [self.managedObjectContext save:&error];
if (!savedSuccessfully) {
    NSLog(@"Could not save date change! Reason : %@", [error localizedDescription]);
}

You should save often and not only when exiting the app. See this answer: How often should I save to Core Data?

When I do a change to the products, remove one or add, do I need to do anything then such as invoking some methods?

This depends on the delete rule you set in the entity in the core data model.

  • nullify: any other object in the relationshipto the deleted object will have those relationship set to nil. for to-many relationships the delted object will just be removed from the collection.
  • cascade: any other object with a relationship to the object is deleted too.
  • deny: the delete will ne denied if there are any other related objects.
  • no action: any other object with a relation to the object will be left unchanged.

Upvotes: 1

Related Questions