Core Data 应用开发全解析:从基础操作到项目搭建
1. Core Data 基础操作
在使用 Core Data 时,插入新对象的操作有些特别,需要使用 NSEntityDescription 的类方法,而非要插入对象的上下文的实例方法。插入操作完成后,虽然托管对象已被插入到上下文,但它仍存在于持久存储中。要将其从持久存储中插入,必须保存上下文,示例代码如下:
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate.
// You should not use this function in a shipping application,
// although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
需要注意的是,在实际应用中,不能简单地调用 abort 来处理错误,后续需要更合适的错误处理方式。而且,在插入新对象后,不
超级会员免费看
订阅专栏 解锁全文
1175

被折叠的 条评论
为什么被折叠?



