一、概述
Core Data框架提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite3数据库文件中,也能够将保存在数据库中的数据还原成OC对象。在此数据操作期间,不需要编写任何SQL语句。使用此功能,要添加CoreData.framework和导入主头文件<CoreData/CoreData.h>。
二、相关内容及功能
1、NSManagedObject
通过Core Data从数据库取出的对象,默认情况下都是NSManagedObject对象,NSManagedObject的工作模式有点类似于NSDictionary对象,通过键-值对来存取所有的实体属性。
(1)setValue:forKey: 存储属性值(属性名为key)
(2)valueForKey: 获取属性值(属性名为key)
2、NSManagedObjectContext
负责应用与数据库之间的交互,增删改查基本操作都要用到
3、NSManagedObjectModel
被管理的数据模型,可以添加实体及实体的属性,若新建的项目带CoreData,即为XXX.xcdatamodeld
4、NSPersistentStoreCoordinator
数据库的连接器,设置数据存储的名字,位置,存储方式等
5、NSFetchRequest
获取数据时的请求
6、NSEntityDescription
用来描述实体
三、模型文件
在Core Data,需要进行映射的对象称为实体(entity),而且需要使用Core Data的模型文件来描述应用的所有实体和实体属性。
这里以Person和Card(身份证)2个实体为例子,先看看实体和实体属性之间的关联关系:
Person中有个Card属性,Card中有个Person属性,属于一对一双向关联。
四、使用方法
下面我们将针对Person和Card的关联关系为例来学习CoreData的使用方法:
1、创建CoreData工程及添加实体和属性
我们新建一个名为CoreDataTest工程,新建工程时勾选Use Core Data,则AppDelegate.h中:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong) NSPersistentContainer *persistentContainer;
- (void)saveContext;
@end
在AppDelegate.m中(为了精简,去除了注释和没用到的方法):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#import "AppDelegate.h"
@interfaceAppDelegate()
@end
@implementationAppDelegate
-(void)applicationWillTerminate:(UIApplication*)application{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
[selfsaveContext];
}
#pragma mark - Core Data stack
@synthesizepersistentContainer=_persistentContainer;
-(NSPersistentContainer*)persistentContainer{
@synchronized(self){
if(_persistentContainer==nil){
_persistentContainer=[[NSPersistentContaineralloc] initWithName:@"CoreDataTest"];
[_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription*storeDescription,NSError*error){
if(error!=nil){
NSLog(@"Unresolved error %@, %@",error,error.userInfo);
abort();
}
}];
}
}
return_persistentContainer;
}
#pragma mark - Core Data Saving support
-(void)saveContext{
NSManagedObjectContext*context=self.persistentContainer.viewContext;
NSError*error=nil;
if([contexthasChanges]&&![context save:&error]){
maybeusefulduringdevelopment.
NSLog(@"Unresolved error %@, %@",error,error.userInfo);
abort();
}
}
@end
不过,这里为了深入理解,我们不用AppDelegate中自动生成的一些方法和属性,我们从零开始自己写,等掌握具体用法后,你自然会知道如何用AppDelegate中自动生成的方法和属性。
建好后你会发现工程中多了CoreDataTest.xcdatamodeld,我们需要在这里添加实体(首字母大写)和实体的属性。
这里我们需要创建Person和Card的实体以及实体属性:
选中Person实体,在Person中添加card属性:
选中Card实体,在Card中添加person属性:
添加完成后,他们关系如下:
2、搭建上下文环境
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
搭建Core Data上下文环境
*/
-(void)initContext{
//从应用程序包中加载模型文件
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
//传入模型,初始化NSPersistentStoreCoordinator:
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
//构建SQLite文件路径:
NSString *docs =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *url = [NSURL fileURLWithPath:[docs stringByAppendingPathComponent:@"person.data"]];
//添加持久化存储库,这里使用SQLite作为存储库:
NSError *error = nil;
NSPersistentStore *store = [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error];
if (store == nil) { // 直接抛异常
[NSException raise:@"添加数据库错误!" format:@"%@", [error localizedDescription]];
}else{
NSLog(@"添加数据库成功!");
}
//初始化上下文,设置persistentStoreCoordinator属性:
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
context.persistentStoreCoordinator = psc;
}
其中
持久化存储库的类型(addPersistentStoreWithType:参数):
(1)NSSQLiteStoreType SQLite数据库
(2)NSBinaryStoreType 二进制平面文件
(3)NSInMemoryStoreType 内存库,无法永久保存数据
ConcurrencyType可选项(initWithConcurrencyType:参数):
(1)NSConfinementConcurrencyType 这个是默认项,每个线程一个独立的Context,主要是为了兼容之前的设计。
(2)NSPrivateQueueConcurrencyType 创建一个private queue(使用GCD),这样就不会阻塞主线程。
(3)NSMainQueueConcurrencyType 创建一个main queue,使用主线程,会阻塞。
3、增:增加数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
增加数据
*/
-(void)addData{
//传入上下文,创建一个Person实体对象:
NSManagedObject*person=
[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:_context];
//设置简单属性:
[person setValue:@"lifengfeng" forKey:@"name"];
[person setValue:[NSNumber numberWithInt:23] forKey:@"age"];
//传入上下文,创建一个Card实体对象:
NSManagedObject*card=[NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:_context];
[card setValue:@"1234567890" forKey:@"no"];
//设置Person和Card之间的关联关系:
[person setValue:card forKey:@"card"];
//利用上下文对象,将数据同步到持久化存储库:
NSError*error=nil;
BOOLsuccess=[_context save:&error];
if(!success){
[NSException raise:@"访问数据库错误!" format:@"%@",[errorlocalizedDescription]];
}else{
NSLog(@"访问数据库成功!");
}
// 如果是想做更新操作:只要在更改了实体对象的属性后调用[context save:&error],就能将更改的数据同步到数据库
}
4、删:删除数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
删除数据
*/
-(void)deleteData{
//建立请求,连接实体
NSFetchRequest *request = [[NSFetchRequest alloc] init] ;
NSEntityDescription *person = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:_context];
request.entity = person;
//设置条件过滤(搜索name属性中包含”lifengfeng“的那条记录,注意等号必须加,可以有空格,也可以是==)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@", @"lifengfeng"];
request.predicate = predicate;
//遍历所有实体,将每个实体的信息存放在数组中
NSArray *arr = [_context executeFetchRequest:request error:nil];
//删除并保存
if(arr.count)
{
for (NSEntityDescription *p in arr)
{
[_context deleteObject:p];
NSLog(@"删除%@成功!",p.name);
}
//保存
[_context save:nil];
}
}
5、改:修改数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
修改数据
*/
-(void)updateData{
//建立请求,连接实体
NSFetchRequest*request=[[NSFetchRequestalloc] init];
NSEntityDescription*person=[NSEntityDescription entityForName:@"Person" inManagedObjectContext:_context];
request.entity=person;
//设置条件过滤(搜索所有name属性不为“lifengfeng”的数据)
NSPredicate*predicate=[NSPredicate predicateWithFormat:@"name!=%@",@"lifengfeng"];
request.predicate=predicate;
//遍历所有实体,将每个实体的信息存放在数组中
NSArray*arr=[_context executeFetchRequest:request error:nil];
//更改并保存
if(arr.count)
{
for(NSEntityDescription*pinarr)
{
p.name=@"更改";
}
//保存
[_context save:nil];
}
else
{
NSLog(@"无检索");
}
}
6、查:查询数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
查询数据
*/
-(void)queryData{
//初始化一个查询请求:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
//设置要查询的实体:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:_context];
request.entity = entity;
//设置排序(按照age降序):
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
request.sortDescriptors = [NSArray arrayWithObject:sort];
//设置条件过滤(name like '%lifengfeng%'):
//设置条件过滤时,数据库里面的%要用*来代替
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like %@", @"*lifengfeng*"];
request.predicate = predicate;
//执行请求:
NSError *error = nil;
NSArray *objs = [_context executeFetchRequest:request error:&error];
if (error) {
[NSException raise:@"查询错误" format:@"%@", [error localizedDescription]];
}
//遍历数据:
for (NSManagedObject *obj in objs) {
NSLog(@"name=%@", [obj valueForKey:@"name"]);
}
}
五、CoreData与多线程
1、Notification实现CoreData并行
CoreData中的NSManagedObjectContext在多线程中不安全,在多线程编程环境下,容易出现每个上下文数据不一致问题。如果想要多线程访问CoreData的话,最好的方法是一个线程一个NSManagedObjectContext,每个NSManagedObjectContext对象实例都可以使用同一个 NSPersistentStoreCoordinator实例,在每一个线程对应一个NSManagedObjectContext的时候,尽量一个线程只读写与其对应的context。在其完成操作的时候通知另外的线程去修改其对应的context。
CoreData里面还带有一个通知NSManagedObjectContextDidSaveNotification,主要监听NSManagedObjectContext的数据是否改变,它可以帮助我们通知修改其它的context,合并数据改变到相应context。
如下图:
例如,我们创建一个主队列的mainContext,主要用于UI操作。一个私有队列的backgroundContext,用于除UI之外的耗时操作,两个Context使用的同一个NSPersistentStoreCoordinator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// 获取psc实例对象
-(NSPersistentStoreCoordinator *)persistentStoreCoordinator{
// 创建托管对象模型,并指明加载Company模型文件
NSURL *modelPath=[[NSBundlemainBundle]URLForResource:@"Company"withExtension:@"momd"];
NSManagedObjectModel *model=[[NSManagedObjectModelalloc]initWithContentsOfURL:modelPath];
// 创建psc对象,并将托管对象模型当做参数传入,其他Context都是用这一个psc。
NSPersistentStoreCoordinator *psc=[[NSPersistentStoreCoordinatoralloc]initWithManagedObjectModel:model];
// 根据指定的路径,创建并关联本地数据库
NSString *dataPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).lastObject;
dataPath=[dataPathstringByAppendingFormat:@"/%@.sqlite",@"Company"];
[pscaddPersistentStoreWithType:NSSQLiteStoreTypeconfiguration:nilURL:[NSURLfileURLWithPath:dataPath]options:nilerror:nil];
returnpsc;
}
// 初始化用于本地存储的所有Context
-(void)createManagedObjectContext{
// 创建psc实例对象,其他Context都用这一个psc。
NSPersistentStoreCoordinator *psc=self.persistentStoreCoordinator;
// 创建主队列Context,用于执行UI操作
NSManagedObjectContext *mainContext=[[NSManagedObjectContextalloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
mainMOC.persistentStoreCoordinator=psc;
// 创建私有队列Context,用于执行其他耗时操作
NSManagedObjectContext *backgroundContext=[[NSManagedObjectContextalloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
backgroundMOC.persistentStoreCoordinator=psc;
// 通过监听NSManagedObjectContextDidSaveNotification通知,来获取所有Context的改变消息
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(contextChanged:)name:NSManagedObjectContextDidSaveNotificationobject:nil];
}
// Context改变后的通知回调
-(void)contextChanged:(NSNotification *)noti{
NSManagedObjectContext *context=noti.object;
// 这里需要做判断操作,判断当前改变的Context是否我们将要做同步的Context,如果就是当前Context自己做的改变,那就不需要再同步自己了。
// 由于项目中可能存在多个psc,所以下面还需要判断psc是否当前操作的psc,如果不是当前psc则不需要同步,不要去同步其他本地存储的数据。
[contextperformBlock:^{
// 直接调用系统提供的同步API,系统内部会完成同步的实现细节。
[contextmergeChangesFromContextDidSaveNotification:noti];
}];
}
在上面的Demo中,创建了一个psc,并将其他Context都关联到这个psc上,这样所有的Context执行本地持久化相关的操作时,都是通过同一个psc进行操作的。并在下面添加了一个通知,这个通知是监听所有Context执行save操作后的通知,并在通知的回调方法中进行数据的合并。
2、parent/child context实现CoreData并行
在iOS5之后,Context可以设置parentContext,一个parentContext可以拥有多个ChildContext。在ChildContext执行save操作后,会将操作push到parentContext,由parentContext去完成真正的save操作,而ChildContext所有的改变都会被parentContext所知晓,这解决了Context手动同步数据的问题。
需要注意的是,在ChildContext调用save方法之后,此时并没有将数据写入存储区,还需要调用parentContext的save方法。因为ChildContext并不拥有psc,ChildContext也不需要设置psc,所以需要parentContext调用psc来执行真正的save操作。也就是只有拥有psc的Context执行save操作后,才是真正的执行了写入存储区的操作。
parent/child context的结构图如下:
这其中有几点要注意
- 通常主线程context使用NSMainQueueConcurrencyType,其他线程childContext使用NSPrivateQueueConcurrencyType.
- child和parent的特点是要用Block进行操作,
performBlock
,或者performBlockAndWait
,保证线程安全。这两个函数的区别是performBlock
不会阻塞运行的线程,相当于异步操作,performBlockAndWait
会阻塞运行线程,相当于同步操作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
-(void)createManagedObjectContext{
// 创建psc实例对象,还是用上面Demo的实例化代码
NSPersistentStoreCoordinator *psc=self.persistentStoreCoordinator;
// 创建主队列Context,用于执行UI操作
NSManagedObjectContext *mainContext=[[NSManagedObjectContextalloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
mainContext.persistentStoreCoordinator=psc;
// 创建私有队列Context,用于执行其他耗时操作,backgroundContext并不需要设置psc
NSManagedObjectContext *backgroundContext=[[NSManagedObjectContextalloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
backgroundContext.parentContext=mainContext;
// 私有队列的Context和主队列的Context,在执行save操作时,都应该调用performBlock:方法,在自己的队列中执行save操作。
// 私有队列的Context执行完自己的save操作后,还调用了主队列Context的save方法,来完成真正的持久化操作,否则不能持久化到本地
[backgroundContextperformBlock:^{
[backgroundContextsave:nil];
[mainContextperformBlock:^{
[mainContextsave:nil];
}];
}];
}
上面例子中创建一个主队列的mainContext,来完成UI相关的操作。创建私有队列的backgroundContext,处理复杂逻辑以及数据处理操作,在实际开发中可以根据需求创建多个backgroundContext。需要注意的是,在backgroundContext执行完save方法后,又在mainContext中执行了一次save方法,这步是很重要的。
Core Data的延迟加载
Core Data不会根据实体中的关联关系立即获取相应的关联对象,比如通过Core Data取出Person实体时,并不会立即查询相关联的Card实体;当应用真的需要使用Card时,才会查询数据库,加载Card实体的信息。
本文示例项目源码下载:
云盘下载
0 0