数据存储(data store)
Core Data支持4中类型的数据存储:SQLiteStore, XMLStore, BinaryStore, InMemoryStore。
1、CoreData使用过程
一、建立CoreData文件(.xcdatamodel)model.xcdatamodeld编译以后会变成model.momd
建立CoreData文件,能够在建立项目的时候勾选“create CoreData”,或者本身手动添加
二、将Entity转成类
导出效果:
//-----------------------------------------model介绍---------------------------------------------
Model class
模型有点像数据库的表结构,里面包含Entry,实体又包含三种Property(特性):
Attribute(属性)
RelationShip(关系)
Fetched Property(读取属性)。
Model class的名字多以 "Description" 结尾。咱们能够看出:模型就是描述数据类型以及其关系的。
Managed Object Model
|
NSManagedObjectModel
|
数据模型
|
Entity |
NSEntityDescription |
抽象数据类型,至关于数据库中的表 |
Property |
NSPropertyDescription |
Entity 特性,至关于数据库表中的一列 |
> Attribute |
NSAttributeDescription |
基本数值型属性(如Int16, BOOL, Date等类型的属性) |
> Relationship |
NSRelationshipDescription |
属性之间的关系 |
> Fetched Property |
NSFetchedPropertyDescription |
查询属性(至关于数据库中的查询语句) |
1)Entity - NSEntityDescription
Entity 至关于数据库中的一个表,它描述一种抽象数据类型,其对应的类为 NSManagedObject 或其子类。
NSEntityDescription 经常使用方法:
+insertNewObjectForEntityForName:inManagedObjectContext: 工厂方法,根据给定的Entity描述,生成相应的NSManagedObject对象,并插入 ManagedObjectContext 中。
-managedObjectClassName:返回映射到Entity的NSManagedObject类名
-attributesByName:以名字为key,返回Entity中对应的Attributes
-relationshipsByName:以名字为key,返回Entity中对应的Relationships
2)Property - NSPropertyDescription
Property为Entity的特性,它至关于数据库表中的一列,或者XML文件中的value-key对中的key。它能够描述实体数据(Attribute),Entity之间的关系(RelationShip),或查询属性(Fetched Property)。
> Attribute - NSAttributeDescription
Attribute 存储基本数据,如 NSString, NSNumber or NSDate 等。它能够有默认值,也可使用正则表达式或其余条件对其值进行限定。一个属性能够是 optional 的。
> Relationship - NSRelationshipDescription
Relationship 描述 Entity,Property 之间的关系,能够是一对一,也能够是一对多的关系。
> Fetched Property - NSFetchedPropertyDescription
Fetched Property 根据查询谓词返回指定 Entity 的符合条件的数据对象。
//-----------------------------------------使用过程---------------------------------------------
手写配置CoreData的对象:
@property (strong, nonatomic)NSManagedObjectContext *context;
- (void)viewDidLoad {
[super viewDidLoad];
//准备工做:建立core data所须要的全部类对象,以及他们之间的关系
建立持久化存储协调器(助理)
//momd:model.xcdatamodeld编译以后会变成model.momd
//1.1 建立模型对象的url
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
//1.2 建立被管理对象模型
NSManagedObjectModel *objectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
真正建立持久化存储协调器对象,并和被管理对象进行绑定
NSPersistentStoreCoordinator *storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:objectModel];
//3.1 由持久化存储协调器对象建立建立出持久化存储对象(真正用来存储数据的路径)
//获取沙盒Documents路径
NSString *docmentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//拼接真正的sqlite文件路径
NSString *sqlitePath = [docmentPath stringByAppendingPathComponent:@"Model.sqlite"];
NSURL *sqliteURL = [NSURL fileURLWithPath:sqlitePath];
NSLog(@"sqlite路径是:%@", sqlitePath);
NSError *error = nil;
//注:可使用匿名方法(并且是推荐使用)
NSPersistentStore *store = [storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:sqliteURL options:nil error:&error];
if (!store) {
NSLog(@"error: %@",[error userInfo]);
}
建立被管理对象上下文
self.context = [[NSManagedObjectContext alloc] init];
//将建立好的持久化存储协调器和被管理上下文进行绑定
[self.context setPersistentStoreCoordinator:storeCoordinator];
}
//------------------------------------------------------系统自动配置-----------------------------------------------
在建立项目的时候,勾选Use CoreData,系统会自动配置CoreData的对象。
由于咱们可使用的就是上下文,所以获取上下文对象就能够了
//被管理对象上下文属性
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
//使用懒加载的方式获取被管理对象上下文
- (NSManagedObjectContext *)managedObjectContext {
//若是对象为空
//获取当前程序的app单例对象:[UIApplication sharedApplication]
//获取当前程序的delegate单例对象:[[UIApplication sharedApplication] delegate]
if (!_managedObjectContext) {
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
//将delegate单例对象的上下文属性,赋值给当前类的上下文对象
_managedObjectContext = delegate.managedObjectContext;
}
return _managedObjectContext;
}
//------------------------------------------------------End------------------------------------------------------
//插入一条数据
- (IBAction)insertItem {
//sql语句insert into person (name, age, height) values ('bob', 19, 1.85);
//1.返回一个给定Entity和被管理对象上下文的“空的”实体对象
PersonModel * person = [NSEntityDescription insertNewObjectForEntityForName:@"PersonModel" inManagedObjectContext:self.context];
//2.将上面返回的空的实体对象进行赋值
person.name = @"Bob";
person.age = @21;
person.height = @1.85;
//3. 使用被管理对象上下文来执行插入动做(save:真正的将数据写入数据库文件中:Model.sqlite)
NSError *error = nil;
[self.context save:&error];
if (error) {
NSLog(@"插入数据失败,失败缘由是:%@", error);
}
}