1.新建项目不勾选coredata
2.pod 'MagicalRecord'
3.新建模型文件
4.添加实体和属性
5.Create NSManagedObject subclass
6.增
Person *person1 = [Person MR_createEntity];
person1.firstname = @"Frank";
person1.lastname = @"Zhang";
person1.age = @25;
Person *person2 = [Person MR_createEntity];
person2.firstname = @"Fra";
person2.lastname = @"ng";
person2.age = @24;
[[NSManagedObjectContext MR_defaultContext]MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) {
NSLog(@"saveOK");
}];
7.查
NSArray * persons = [Person MR_findAll];
NSArray * personsSorted = [Person MR_findAllSortedBy:@"firstname" ascending:YES];
NSArray * personsAgeEuqals25 = [Person MR_findByAttribute:@"age" withValue:[NSNumber numberWithInt:25]];
Person *firstperson = [Person MR_findFirst];
8.删
Person *person = [Person MR_findAll].firstObject;
[person MR_deleteEntity];
[[NSManagedObjectContext MR_defaultContext]MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) {
NSLog(@"saveOK");
}];
9.改
Person *person = [Person MR_findAll].firstObject;
person.lastname = @'"fdbsfb";
[[NSManagedObjectContext MR_defaultContext]MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) {
NSLog(@"saveOK");
}];