核心数据:将编辑保存到持久存储

时间:2015-05-24 19:27:47

标签: ios core-data

我想我知道答案但不确定。而且我没有很好的方法来检查因为它还没有正常工作,所以因此提问。

为了显示存储在核心数据的持久存储(即sqllite数据库)中的用户简档,我使用NSpredicate从userinfo的实体或表中获取对应于该用户的对象。这会拉出一个用户并将其加载到托管的objectcontext中,以便我可以显示它。到目前为止一切都很好。

但是,要编辑信息,我只需要更改managedobjectcontext中的信息并保存,并且不需要在save中具有任何等效的WHERE子句。换句话说,这样做是否足够:

[self.user setValue:_description forKey:@"descript"];
 if ([self.managedObjectContext save:&error]) {
            NSLog(@"we saved");
      }

不指定我们正在编辑的持久存储中的哪个托管对象或行?

1 个答案:

答案 0 :(得分:0)

Short answer - yes, it is enough to call save:. As long as the changed object is a member of the receiving managedObjectContext and the call returns YES the save has been done.

NSManagedObjectContext neither knows nor cares about its backing store type.

NSPersistentStore is the thing that knows about its backing store type and deals with the mechanics of transacting with the SQLite database when it's set to NSSQLiteStoreType

You can have 3 types of backing store on iOS and 4 on OS X and stores of all those types can be loaded into the NSPersistentStoreCoordinator at the same time. Probably not a good idea but technically possible.

If you are feeling particularly adventurous you can make your own store type via NSIncrementalStore.