一 准备工作
1 安装MagicalRecord,参照上篇文章 http://ios2013yoyo.blog.51cto.com/6260005/1662583
2 在pch中#import相关文件
#import <MagicalRecord/MagicalRecord.h>
#import <MagicalRecord/MagicalRecord+ShorthandMethods.h>
#import <MagicalRecord/MagicalRecordShorthandMethodAliases.h>
3 在appdelte建立sqlite,并创建mod,model等文件。参照 http://ios2013yoyo.blog.51cto.com/6260005/1662281
二 使用MagicalRecord
//创建实体 - (IBAction)add:(id)sender { Person *person = [Person MR_createEntity]; person.firstname = @"Frank"; person.lastname = @"Zhang"; person.age = @23;//此处使用了LLVM的新特性,XCode 4.4可用 [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait]; }
//删除 - (IBAction)delete:(id)sender { Person *person = [Person MR_findFirst]; [person MR_deleteEntity]; [[NSManagedObjectContext defaultContext] MR_saveToPersistentStoreAndWait]; /* 删除特定上下文中的实体 [myPerson MR_deleteInContext:otherContext]; 删除所有实体 [Person MR_truncateAll]; 删除特定上下文中的所有实体 [Person MR_truncateAllInContext:otherContext]; */ }
//更新 - (IBAction)change:(id)sender { Person *person = [Person MR_findFirst]; person.age = @27; [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait]; }
//查询 - (IBAction)qury:(id)sender { //查找数据库中的所有Person。 NSArray *persons = [Person MR_findAll]; for (Person *per in persons) { NSLog(@"%@",per.firstname); }
/* 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"]; */ }
//高级查询 - (IBAction)predicateQury:(id)sender { /* 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]; */ }
//个数查询(sum,max) - (IBAction)numQury:(id)sender { /* e) 查询实体的个数 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 *)context;
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]; */ }
/
/后台线程存储 - (IBAction)backgroundSave:(id)sender { /* 存储比较耗时,有下面几点你可以考虑下 1.利用后台线程存储 2.将存储任务拆分成细小的存储 */ UIApplication *application = [UIApplication sharedApplication]; __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) { //这里做存储操作 } completion:^(BOOL success, NSError *error) { [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; }
三 demo请查看附件