美文网首页
UI进阶11 Core Data

UI进阶11 Core Data

作者: SoManyDumb | 来源:发表于2016-08-24 21:54 被阅读26次

Core Data

  • Core Data框架提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite3数据库文件中,也能够将保存在数据库中的数据还原成OC对象。在此数据操作期间,不需要编写任何SQL语句。使用此功能,要添加CoreData.framework和导入主头文件<CoreData/CoreData.h>

模型文件

  • 在Core Data,需要进行映射的对象称为实体(entity),而且需要使用Core Data的模型文件来描述应用的所有实体和实体属性

  • 这里以Person和Card(身份证)2个实体为例子,先看看实体属性和之间的关联关系

    • Person中有个Card属性,Card中有个Person属性
    • 属于一对一双向关联
  • 创建文件

    • Core Data - > Data Model
  • 添加实体

    • Add Entity
  • 添加Person实体的基本属性

    • Attributes - > 加号 - > 不勾选 Translent
  • 添加Card实体的基本属性 等等

NSManagedObject

  • 通过Core Data从数据库取出的对象,默认情况下都是NSManagedObject对象
  • NSManagedObject的工作模式有点类似于NSDictionary对象,通过键值对来存取所有的实体属性
    • setValue:forKey: 存储属性值(属性名为key)
    • valueForKey: 获取属性值(属性名为key)

搭建Core Data上下文环境

  • 从应用程序包中加载模型文件
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
  • 传入模型,初始化NSPersistentStoreCoordinator
NSPersistentStoreCoordinator *psc = [[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model] autorelease];
  • 构建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]];
}
  • 初始化上下文,设置persistentStoreCoordinator属性
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
context.persistentStoreCoordinator = psc;
// 用完之后,还是要[context release];

添加数据

  • 传入上下文,创建一个Person实体对象
NSManagedObject *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
[person setValue:@"MJ" forKey:@"name"];
[person setValue:[NSNumber numberWithInt:27] forKey:@"age"];
  • 传入上下文,创建一个Card实体对象
cription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:context];
[card setValue:@"4414241933432" forKey:@"no"];
  • 设置Person和Card之间的关联关系
[person setValue:card forKey:@"card"];
  • 利用上下文对象,将数据同步到持久化存储库
NSError *error = nil;
BOOL success = [context save:&error];
if (!success) {
    [NSException raise:@"访问数据库错误" format:@"%@", [error localizedDescription]];
}
// 如果是想做更新操作:只要在更改了实体对象的属性后调用[context save:&error],就能将更改的数据同步到数据库

查询数据

  • 初始化一个查询请求
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
  • 设置要查询的实体
NSEntityDescription *desc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
  • 设置排序(按照age降序)
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
request.sortDescriptors = [NSArray arrayWithObject:sort];
  • 设置条件过滤(name like '%Itcast-1%')
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like %@", @"*Itcast-1*"];
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"]
}

删除数据

  • 传入需要删除的实体对象
[context deleteObject:managedObject];
  • 将结果同步到数据库
NSError *error = nil;
[context save:&error];
if (error) {
    [NSException raise:@"删除错误" format:@"%@", [error localizedDescription]];
}
  • 打开Core Data的SQL日志输出开关
    • 1.打开Product,点击Edit Scheme...
    • 2.点击Arguments,在Arguments Passed On Launch中添加2项
      -com.apple.CoreData.SQLDebug

Core Data的延迟加载

  • Core Data不会根据实体中的关联关系立即获取相应的关联对象
  • 比如通过Core Data取出Person实体时,并不会立即查询相关联的Card实体;当应用真的需要使用Card时,才会查询数据库,加载Card实体的信息

创建NSManagedObject的子类

  • 默认情况下,利用Core Data取出的实体都是NSManagedObject类型的,能够利用键-值对来存取数据

  • 但是一般情况下,实体在存取数据的基础上,有时还需要添加一些业务方法来完成一些其他任务,那么就必须创建NSManagedObject的子类

  • 那么生成一个Person实体对象就应该这样写

Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
person.name = @"MJ";
person.age = [NSNumber numberWithInt:27];

Card *card = [NSEntityDescription insertNewObjectForEntityForName:@”Card" inManagedObjectContext:context];
card.no = @”4414245465656";
person.card = card;

相关文章

网友评论

      本文标题:UI进阶11 Core Data

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