CoreData的底层实现尽管是使用的sqlite数据库。但是CoreData在使用起来但是和sqlite大相径庭。可能你会发现你连一句sql语句都不要写。CoreData存在于应用程序和持久化存储区之间,扮演了桥梁的角色,将托管的对象映射到持久化存储区其中。

1.设置上下文

在代码開始之前还须要加入CoreData框架,并在合适的地方引入头文件<CoreData/CoreData.h>:

  1. // 从应用程序包中载入模型文件
  2. NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
  3. // 传入模型对象。初始化NSPersistentStoreCoordinator
  4. NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
  5. // 构建SQLite数据库文件的路径
  6. NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"model.data"];
  7. // 将数据库路径转成URL
  8. NSURL *url = [NSURL fileURLWithPath:filePath];
  9. // 加入持久化存储库,这里使用SQLite作为存储库
  10. NSError *error = nil;
  11. NSPersistentStore *store = [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error];
  12. // 推断数据库是否加入成功
  13. if (store == nil) {
  14. [NSException raise:@"加入数据库错误" format:@"%@", [error localizedDescription]];
  15. }
  16. // 初始化上下文
  17. NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
  18. // 设置persistentStoreCoordinator属性
  19. context.persistentStoreCoordinator = psc;

2.加入数据

  1. // 创建一个Husband实体对象,传入上下文
  2. NSManagedObject *husband = [NSEntityDescription insertNewObjectForEntityForName:@"Husband" inManagedObjectContext:context];
  3. // 通过键值编码的方式设置Husband的name属性
  4. [husband setValue:@"jack" forKey:@"name"];
  5. // 通过coredata生成的实体类来创建一个Wife实体对象,传入上下文
  6. Wife *wife = [NSEntityDescription insertNewObjectForEntityForName:@"Wife" inManagedObjectContext:context];
  7. // 通过setter方法设置属性
  8. wife.name = @"rose";
  9. // 设置Husband和Wife之间的关联关系(一方关联,还有一方自己主动关联)
  10. wife.husband = husband;
  11. // 利用上下文对象,将数据同步到持久化存储库
  12. BOOL success = [context save:&error];
  13. if (!success) {
  14. [NSException raise:@"訪问数据库错误" format:@"%@", [error localizedDescription]];
  15. }
  16. // 假设是想做更新操作:须要将实体对象先查询出来。在更改了实体对象的属性后调用[context save:&error],就能将更改的数据同步到数据库

3.查询数据

  1. // 初始化一个查询请求
  2. NSFetchRequest *request = [[NSFetchRequest alloc] init];
  3. // 设置要查询的实体
  4. request.entity = [NSEntityDescription entityForName:@"Husband" inManagedObjectContext:context];
  5. // 设置排序(依照name降序)
  6. NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO];
  7. request.sortDescriptors = [NSArray arrayWithObject:sort];
  8. // 设置条件过滤(搜索name中包括字符串"ja"的记录)
  9. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like %@", @"*ja*"];
  10. request.predicate = predicate;
  11. // 运行请求,返回一个数组
  12. NSArray *objs = [context executeFetchRequest:request error:&error];
  13. if (error) {
  14. [NSException raise:@"查询错误" format:@"%@", [error localizedDescription]];
  15. }
  16. // 遍历数据
  17. for (NSManagedObject *obj in objs) {
  18. NSLog(@"name=%@", [obj valueForKey:@"name"]);
  19. // 实体属性中包括还有一个实体。不须要再次设置查询请求,Core Data会依据关联关系查询到关联的实体信息
  20. NSLog(@"wife = %@", [[obj valueForKey:@"wife"] valueForKey:@"name"]);
  21. }

fetchRequest相当于sql查询语句的包装类。须要用setEntity方法,来指定详细查询的实体结构(表结构);

通过NSEntityDescription的entityForName方法来。返回指向该详细实体结构的指针;

然后调用executeFetchRequest:error:方法,来运行查询操作,假设操作成功,则返回相应的数据记录数组。

当中,能够通过NSManagedObject数据记录对象里关联的属性,查询还有一个数据记录对象里的属性;

CoreData不会依据实体中的关联关系马上获取对应的关联对象,比方通过CoreData取出Husband实体时。并不会马上查询相关联的Wife实体;当应用真的须要使用Wife时,才会再次查询数据库。载入Wife实体的信息。这个就是CoreData的延迟载入机制。

4.删除数据

Core Data的增删改使用的方法都是save:方法,在上下文调用save方法之前,全部的数据改动都是发生在内存中的。仅仅有调用save方法后,上下文中发生的数据改动才会被写入到持久化存储区。

获取到须要删除的实体对象之后。调用deleteObject:方法就能够从上下文中删除这个实体对象了,最后须要调用save:方法同步改动到数据库中:

  1. // 初始化一个查询请求
  2. NSFetchRequest *request = [[NSFetchRequest alloc] init];
  3. // 设置要查询的实体
  4. request.entity = [NSEntityDescription entityForName:@"Husband" inManagedObjectContext:context];
  5. // 设置条件过滤(搜索name等于jack2的实体)
  6. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", @"jack2"];
  7. request.predicate = predicate;
  8. // 运行请求,返回一个数组
  9. NSArray *objs = [context executeFetchRequest:request error:&error];
  10. if (error) {
  11. [NSException raise:@"查询错误" format:@"%@", [error localizedDescription]];
  12. }
  13. // 遍历数据
  14. for (NSManagedObject *obj in objs) {
  15. // 传入须要删除的实体对象
  16. [context deleteObject:obj];
  17. // 将结果同步到数据库
  18. [context save:&error];
  19. if (error) {
  20. [NSException raise:@"删除错误" format:@"%@", [error localizedDescription]];
  21. }
  22. }

5.新建project时勾选Use Core Data选项的情况

在新建project时使用CoreData,系统会帮我们在AppDelegate中搭建好一个上下文环境,我们能够在其它的controller中去使用这个context,省去了自己搭建上下文的操作,使用起来很简便。

AppDelegate.h中:

  1. @interface AppDelegate : UIResponder
  2. @property (strong, nonatomic) UIWindow *window;
  3. // 搭建上下文环境须要使用的对象
  4. @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
  5. @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
  6. @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
  7. // 保存实体对象到数据库中
  8. - (void)saveContext;
  9. // 取得程序沙盒路径的URL
  10. - (NSURL *)applicationDocumentsDirectory;
  11. @end

AppDelegate.m中:

  1. #pragma mark - Core Data stack
  2. @synthesize managedObjectContext = _managedObjectContext;
  3. @synthesize managedObjectModel = _managedObjectModel;
  4. @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
  5. - (NSURL *)applicationDocumentsDirectory {
  6. // The directory the application uses to store the Core Data store file. This code uses a directory named "edu.hcit.qqqqq" in the application's documents directory.
  7. return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
  8. }
  9. - (NSManagedObjectModel *)managedObjectModel {
  10. // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
  11. if (_managedObjectModel != nil) {
  12. return _managedObjectModel;
  13. }
  14. /**************************************************************************************************/
  15. // model 是模型文件的名称,默认是和项目名称同样的
  16. NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"model" withExtension:@"momd"];
  17. _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
  18. return _managedObjectModel;
  19. }
  20. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
  21. // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it.
  22. if (_persistentStoreCoordinator != nil) {
  23. return _persistentStoreCoordinator;
  24. }
  25. // Create the coordinator and store
  26. _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
  27. /**************************************************************************************************/
  28. // 以下的数据库 model.sqlite 是存储实体数据的数据库
  29. NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"model.sqlite"];
  30. NSError *error = nil;
  31. NSString *failureReason = @"There was an error creating or loading the application's saved data.";
  32. if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
  33. // Report any error we got.
  34. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  35. dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
  36. dict[NSLocalizedFailureReasonErrorKey] = failureReason;
  37. dict[NSUnderlyingErrorKey] = error;
  38. error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
  39. // Replace this with code to handle the error appropriately.
  40. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
  41. NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  42. abort();
  43. }
  44. return _persistentStoreCoordinator;
  45. }
  46. - (NSManagedObjectContext *)managedObjectContext {
  47. // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
  48. if (_managedObjectContext != nil) {
  49. return _managedObjectContext;
  50. }
  51. NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
  52. if (!coordinator) {
  53. return nil;
  54. }
  55. _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
  56. [_managedObjectContext setPersistentStoreCoordinator:coordinator];
  57. return _managedObjectContext;
  58. }
  1. #pragma mark - Core Data Saving support
  2. - (void)saveContext {
  3. NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
  4. if (managedObjectContext != nil) {
  5. NSError *error = nil;
  6. if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
  7. // Replace this implementation with code to handle the error appropriately.
  8. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
  9. NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  10. abort();
  11. }
  12. }
  13. }

假设在一个已有的project中加入CoreData。搭建上下文时能够新建一个使用CoreData的project,将上述的代码复制到已有project,在AppDelegate.m中将模型文件的名称和数据库名称稍作改动就可以。数据的操作方法与上文类似。

6.打印隐藏的SQL语句

在Edit Scheme中选择Run,之后进入Arguments标签,加入參数:“-com.apple.CoreData.SQLDebug 1”

打开SQL语句隐藏开关后,程序在执行时。debug日志里会打印程序执行的SQL语句:

Coredata — 入门使用的更多相关文章

  1. iphone dev 入门实例4:CoreData入门

    The iPhone Core Data Example Application The application developed in this chapter will take the for ...

  2. CoreData 从入门到精通(五)CoreData 和 TableView 结合

    我们知道 CoreData 里存储的是具有相同结构的一系列数据的集合,TableView 正好是用列表来展示一系列具有相同结构的数据集合的.所以,要是 CoreData 和 TableView 能结合 ...

  3. CoreData 从入门到精通(六)模型版本和数据迁移

    前面几篇文章中讲的所有内容,都是在同一个模型版本上进行操作的.但在真实开发中,基本上不会一直停留在一个版本上,因为需求是不断变化的,说不定什么时候就需要往模型里添加新的字段,添加新的模型,甚至是大规模 ...

  4. CoreData 从入门到精通(四)并发操作

    通常情况下,CoreData 的增删改查操作都在主线程上执行,那么对数据库的操作就会影响到 UI 操作,这在操作的数据量比较小的时候,执行的速度很快,我们也不会察觉到对 UI 的影响,但是当数据量特别 ...

  5. CoreData 从入门到精通(三)关联表的创建

    上篇博客中讲了 CoreData 里增删改查的使用,学到这里已经可以应对简单的数据存储需求了.但是当数据模型复杂起来时,例如你的模型类中除了要存储 CoreData 里支持的数据类型外,还有一些自定义 ...

  6. CoreData 从入门到精通(二) 数据的增删改查

    在上篇博客中,讲了数据模型和 CoreData 栈的创建,那下一步就是对数据的操作了.和数据库一样,CoreData 里的操作也无非是增删改查.下面我们将逐步讲解在 CoreData 中进行增删改查的 ...

  7. CoreData 从入门到精通 (一) 数据模型 + CoreData 栈的创建

    CoreData 是 Cocoa 平台上用来管理模型层数据和数据持久化的一个框架,说简单点,就是一个数据库存储框架.CoreData 里相关的概念比较多,而且初始化也非常繁琐,所以对初学者的学习还是有 ...

  8. CoreData的使用入门到精通

    源码下载地址: http://download.csdn.net/download/huntaiji/6664567 一,创建项目文件--选择Empty Application  起名:CoreDat ...

  9. Core Data浅谈初级入门

    Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对象.在此数 ...

随机推荐

  1. win 10 文件夹 背景 没效果

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha  313134555@qq.com

  2. hdu 3864 素数分解

    题意:求n是否只有4个因子,如果是就输出除1外的所有因子. 模板题,就不排版了 #include<cstdio> #include<iostream> #include< ...

  3. sgu 261

    学习了元根的一些知识,哈哈. 总结一下: 几个概念: 阶:对于模数m和整数a,并且gcd(m,a)==1,那么定义a在模m下的阶r为满足ar=1 mod m的最小正整数. 性质1:r in [1,ph ...

  4. Java发送HTTP POST请求示例

    概述: http请求在所有的编程语言中几乎都是支持的,我们常用的两种为:GET,POST请求.一般情况下,发送一个GET请求都很简单,因为参数直接放在请求的URL上,所以,对于PHP这种语言,甚至只需 ...

  5. window.open如何返回值给父窗口

    父窗口代码 function showMyWindowNew() { var iTop = (window.screen.availHeight - 30 - 450) / 2; //获得窗口的水平位 ...

  6. URAL 1876 Centipede's Morning

    1876. Centipede's Morning Time limit: 0.5 secondMemory limit: 64 MB A centipede has 40 left feet and ...

  7. Codeforces Round #296 (Div. 1) C. Data Center Drama 欧拉回路

    Codeforces Round #296 (Div. 1)C. Data Center Drama Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xx ...

  8. UEFI引导模式

    Author: JinDate: 20140827System: windows 刚帮楼下的公司解决了个问题. 原来的办公电脑,预装linux,他们重装成win7.新买的电脑预装成win8,安装出问题 ...

  9. USB Mass Storage大容量存储 The Thirteen Class章节的理解

    http://blog.csdn.net/xgbing/article/details/7002558 USB Mass Storage 6.7 The Thirteen Class章节的理解 Cas ...

  10. what is a process?

    A process is a program in execution. A process is more than the program code, which is sometimes kno ...