自我感觉方便许多
- (void)viewDidLoad {
[super viewDidLoad];
// 查询
[self selectAction];
// 更新
// [self updateAction];
// 删除
[self deleteAction];
// 查询
[self selectAction];
}
-(void)insertAction
{
Person * per = [Person MR_createEntity];
per.name = @"zhangsan";
per.age = @12;
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}
-(void)deleteAction
{
NSArray *persons = [Person MR_findAll];
Person * per = persons[0];
// 删除实体
[per MR_deleteEntity];
// 保存此变化
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}
-(void)updateAction
{
//查找数据库中的所有Person。
NSArray *persons = [Person MR_findAll];
Person * per = persons[0];
per.name = @"lisi";
per.age = @80;
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}
-(void)selectAction
{
//查找数据库中的所有Person。
NSArray *persons = [Person MR_findAll];
NSLog(@"%ld", persons.count);
for(Person * per in persons){
NSLog(@"name = %@,age = %@", per.name, per.age);
}
}