0

如何更新 Core Data 实体,需要哪些操作?我有一个Store一对多的关系Product。当我对产品进行更改、删除或添加时,我是否需要执行任何操作,例如调用某些方法?还是我只是编辑产品并将它们单独放置,然后当用户退出应用程序时将它们保存到持久存储中?这是正常的方式吗?

4

1 回答 1

1

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.
于 2012-05-24T09:18:39.463 回答