深入浅出MagicalRecord-03

这节我们来一起学习下MagicalRecord对数据的增删改查,内容主要来自于 MagicalRecord的github资料

1. 增-创建实体

创建实体
Person *myPerson = [Person MR_createEntity];
指定创建的上下文中创建实体
Person *myPerson = [Person MR_createInContext:otherContext];

2. 删-删除实体

删除一个实体
[myPerson MR_deleteEntity];
删除指定上下文中的实体
[myPerson MR_deleteInContext:otherContext];
删除所有实体
[Person MR_truncateAll];
删除指定上下文中的所有实体
[Person MR_truncateAllInContext:otherContext];

3. 改-修改实体

Person *person = ...;
person.lastname = "xxx";

4. 查-查询实体

查询的结果通常会返回一个 NSArray 结果。

a) 基本查询

从持久化存储(PersistantStore)中查询所有的Person实体
NSArray *people = [Person MR_findAll];
查询出所有的Person实体并按照lastName升序(ascending)排列
NSArray *peopleSorted = [Person MR_findAllSortedBy:@"lastName" ascending:YES];
查询出所有的Person实体并按照lastName,firstName升序(ascending)排列
NSArray *peopleSorted = [Person MR_findAllSortedBy:@"lastName,firstName" ascending:YES];
查询出所有的Person实体并按照lastName升序,firstName降序(ascending)排列
NSArray *peopleSorted = [Person MR_findAllSortedBy:@"lastName:NO,firstName" ascending:YES];
//或者

NSArray *peopleSorted = [Person MR_findAllSortedBy:@"lastName,firstName:YES" ascending:NO];
查询出所有的Person实体,lastName为Forrest的实体
Person *person = [Person MR_findFirstByAttribute:@"firstName" withValue:@"Forrest"];

b) 高级查询

使用NSPredicate来实现高级查询。

NSPredicate *peopleFilter = [NSPredicate predicateWithFormat:@"department IN %@", @[dept1, dept2]];
NSArray *people = [Person MR_findAllWithPredicate:peopleFilter];

c)返回 NSFetchRequest

NSPredicate *peopleFilter = [NSPredicate predicateWithFormat:@"department IN %@", departments];
NSFetchRequest *people = [Person MR_requestAllWithPredicate:peopleFilter];

d)自定义 NSFetchRequest

NSPredicate *peopleFilter = [NSPredicate predicateWithFormat:@"department IN %@", departments];

NSFetchRequest *peopleRequest = [Person MR_requestAllWithPredicate:peopleFilter];
[peopleRequest setReturnsDistinctResults:NO];
[peopleRequest setReturnPropertiesNamed:@[@"firstName", @"lastName"]];

NSArray *people = [Person MR_executeFetchRequest:peopleRequest];

e)查询实体的个数

返回的是NSNumber类型
NSNumber *count = [Person MR_numberOfEntities];
基于NSPredicate查询条件过滤后的实体个数
NSNumber *count = [Person MR_numberOfEntitiesWithPredicate:...];
返回的是NSUInterger类型
+ (NSUInteger) MR_countOfEntities;
+ (NSUInteger) MR_countOfEntitiesWithContext:(NSManagedObjectContext *)context;
+ (NSUInteger) MR_countOfEntitiesWithPredicate:(NSPredicate *)searchFilter;
+ (NSUInteger) MR_countOfEntitiesWithPredicate:(NSPredicate *)searchFilter inContext:(NSManagedObjectContext *)

f)合计操作

NSInteger totalFat = [[CTFoodDiaryEntry MR_aggregateOperation:@"sum:" onAttribute:@"fatCalories" withPredicate:predicate] integerValue];

NSInteger fattest  = [[CTFoodDiaryEntry MR_aggregateOperation:@"max:" onAttribute:@"fatCalories" withPredicate:predicate] integerValue];

NSArray *caloriesByMonth = [CTFoodDiaryEntry MR_aggregateOperation:@"sum:" onAttribute:@"fatCalories" withPredicate:predicate groupBy:@"month"];

g)从指定上下文中查询

NSArray *peopleFromAnotherContext = [Person MR_findAllInContext:someOtherContext];

Person *personFromContext = [Person MR_findFirstByAttribute:@"lastName" withValue:@"Gump" inContext:someOtherContext];

NSUInteger count = [Person MR_numberOfEntitiesWithContext:someOtherContext];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值