美文网首页iOS 干货整理
iOS-MagicalRecord补充

iOS-MagicalRecord补充

作者: oneDemo | 来源:发表于2015-03-30 18:57 被阅读620次

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实体 firstName 为 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:...];

返回的是 NSUInteger 类型

+ (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];

貌似该库中没有嵌套查询,多条件查询。

相关文章

  • iOS-MagicalRecord补充

    1. 增-创建实体 创建实体 Person *myPerson = [Person MR_createEntity...

  • swift笔记:函数补充,枚举补充,属性补充,继承补充

    关键字inout 这个例子是在playground下写的 inout这个形参接收的相当于接收的是结构体变量的地址,...

  • 补充

    一脸倦容的老先生倚靠在凳子上,一手举着茶壶,一手扇着扇子,背后支着面脏得看不清本色的旗子,上面歪歪扭扭地写着“卜”...

  • 补充

    1.生命周期 每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如,需要设置数据监听、编译模板、将实例挂...

  • 补充

  • 补充

    让心很自然的平静下来,不再受外界的打扰,抛开生活中所有的烦恼,放下社会中的地位,将注意力让着我们的身体上,有内而外...

  • 补充

    v-html 可以识别html标签v-text 不可以识别html标签 v-once 只绑定一次v-pre ...

  • 补充

  • 补充

  • 补充

    七月二十号,我和邓候打扫卫生。她从门口进来,候的身体堵住了去厕所的路。 “让一下,我洗个手。”她发出棉花糖般柔...

网友评论

    本文标题:iOS-MagicalRecord补充

    本文链接:https://www.haomeiwen.com/subject/jsfoxttx.html