使用CoreData [4]

此片文章主要是分析如何对CoreData进行封装.

在开始之前,我们需要弄明白3个非常关键的类,以下翻译凑合着看看.

NSManagedObjectContext

An instance of NSManagedObjectContext represents a single “object space” or scratch pad in an application. Its primary responsibility is to manage a collection of managed objects. These objects form a group of related model objects that represent an internally consistent view of one or more persistent stores. A single managed object instance exists in one and only one context, but multiple copies of an object can exist in different contexts. Thus object uniquing is scoped to a particular context.

一个NSManagedObjectContext代表着一个独立的对象空间或者是应用的一个记录暂存区.它主要的职责就是用来管理一系列的对象.这些对象来自于一系列的与model对象相关的组,用来代表一个存储器或者多个存储器.一个管理对象的实例只能有一个上下文,但是这个对象的多份拷贝能够存在不同的上下文.这些对象在特殊的上下文之间有着作用域的不同.

NSManagedObjectModel

An NSManagedObjectModel object describes a schema—a collection of entities (data models) that you use in your application.
一个NSManagedObjectModel对象描述了一种表结构-一个你的应用程序中实体类集合的描述.
The model contains one or more NSEntityDescription objects representing the entities in the schema. Each NSEntityDescription object has property description objects (instances of subclasses of NSPropertyDescription) that represent the properties (or fields) of the entity in the schema. The Core Data framework uses this description in several ways:
这个model包含了一个或者多个NSEntityDescription对象用来代表这个表中的实体类.每一个NSEntityDescription对象都有一个描述的对象,用来代表这个对象在表结构中的实体.Core Data 框架通过以下几种方式来使用这些描述信息:
Constraining UI creation in Interface Builder

在IB中创建UI
Validating attribute and relationship values at runtime

在运行时期间确认属性以及关系
Mapping between your managed objects and a database or file-based schema for object persistence.

在你管理的对像和数据库中的一张表产生映射关系.

NSPersistentStoreCoordinator

Instances of NSPersistentStoreCoordinator associate persistent stores (by type) with a model (or more accurately, a configuration of a model) and serve to mediate between the persistent store or stores and the managed object context or contexts. Instances of NSManagedObjectContext use a coordinator to save object graphs to persistent storage and to retrieve model information. A context without a coordinator is not fully functional as it cannot access a model except through a coordinator. The coordinator is designed to present a façade to the managed object contexts such that a group of persistent stores appears as an aggregate store. A managed object context can then create an object graph based on the union of all the data stores the coordinator covers.

NSPersistentStoreCoordinator的实体对象维系数据库,以及在数据库和对象之间,操作句柄之间进行着通信.NSPersistentStoreCoordinator的实例对象通过协调的方式来将对象本地持久化以及将这个对象从本地中读取出来.
Coordinators do the opposite of providing for concurrency—€”they serialize operations. If you want to use multiple threads for different write operations you use multiple coordinators. Note that if multiple threads work directly with a coordinator, they need to lock and unlock it explicitly.
Coordinators按照序列化操作.如果你想在多线程中使用不同的写操作,你需要使用不同的coordinator.请注意,如果多线程在操作一个coordinator,这些线程就需要通过加锁解锁的方式来保证操作的正确性.
Each coordinator (and thus container) may use different copies, and hence different versions, of a managed object model. This allows you to cleanly deal with file versioning.
每一个coordinator也许会用到不同的拷贝,以及继承不同的版本,这允许你兼容不同的版本.
The coordinator gives access to its underlying object stores. You can retrieve an object store when you first add one (using addPersistentStoreWithType:configuration:URL:options:error:), or by using persistentStoreForURL: or persistentStores. This allows you to to determine, for example, whether a store has already been added, or whether two objects come from the same store.
这个coordinator可以很便利的操作实时的存储.你可以立马就查出一个对象当你第一次添加了coordinator.

我们重新创建一个工程:

创建出CoreData

然后创建出Student类

然后,导入以下几个本人写的文件:

StoreCoordinator.h   +   StoreCoordinator.m

//
// StoreCoordinator.h
// http://home.cnblogs.com/u/YouXianMing/
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <Foundation/Foundation.h> @interface StoreCoordinator : NSObject // 配置表文件以及数据库文件
+ (void)setBundleObjectModel:(NSString *)name setDBPath:(NSString *)path; // 提供coordinator
+ (id)coordinator; @end
//
// StoreCoordinator.m
// http://home.cnblogs.com/u/YouXianMing/
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "StoreCoordinator.h"
#import <CoreData/CoreData.h> static NSPersistentStoreCoordinator *storeCoordinator = nil; @implementation StoreCoordinator + (void)setBundleObjectModel:(NSString *)name setDBPath:(NSString *)path
{
if (storeCoordinator == nil)
{
// 获取bundle中的表结构
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:name
withExtension:@"momd"];
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; // 获取表结构
storeCoordinator = \
[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; // 由表结构与数据库建立联系
[storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:[NSURL fileURLWithPath:path]
options:nil
error:nil];
}
} + (id)coordinator
{
return storeCoordinator;
} @end

ObjectContext.h  +  ObjectContext.m

//
// ObjectContext.h
// http://home.cnblogs.com/u/YouXianMing/
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <Foundation/Foundation.h> @interface ObjectContext : NSObject @property (nonatomic, strong, readonly) NSManagedObjectContext *context; // 从coordinator中初始化一个操作句柄
- (instancetype)initWithCoordinator:(id)coordinator; // 保存这个操作句柄中的所有变化
- (void)saveChanges; @end
//
// ObjectContext.m
// http://home.cnblogs.com/u/YouXianMing/
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "ObjectContext.h"
#import <CoreData/CoreData.h> @interface ObjectContext () @property (nonatomic, strong) NSManagedObjectContext *context; @end @implementation ObjectContext - (instancetype)initWithCoordinator:(id)coordinator
{
self = [super init];
if (self)
{
_context = [[NSManagedObjectContext alloc] init];
[_context setPersistentStoreCoordinator:coordinator];
}
return self;
} - (void)saveChanges
{
NSError *error = nil;
if (_context != nil)
{
if ([_context hasChanges] && ![_context save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
} @end

NSManagedObject+YX.h   +   NSManagedObject+YX.m

//
// NSManagedObject+YX.h
// http://home.cnblogs.com/u/YouXianMing/
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <CoreData/CoreData.h>
@class ObjectContext; @interface NSManagedObject (YX) // 创建一条记录
+ (id)createInContext:(ObjectContext *)context; // 查询所有记录
+ (NSArray *)allInContext:(ObjectContext *)context; // 删除一条记录
- (void)deleteSelf; @end
//
// NSManagedObject+YX.m
// http://home.cnblogs.com/u/YouXianMing/
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "NSManagedObject+YX.h"
#import "ObjectContext.h" @implementation NSManagedObject (YX) + (id)createInContext:(ObjectContext *)context
{
return [NSEntityDescription insertNewObjectForEntityForName:[self entityName]
inManagedObjectContext:context.context];
} + (NSArray *)allInContext:(ObjectContext *)context
{
// 设定要查询的实体
NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:[self entityName]]; // 取出查询结果
return [context.context executeFetchRequest:fetch error:nil];
} + (NSString *)entityName
{
return NSStringFromClass(self);
} - (void)deleteSelf
{
[self.managedObjectContext deleteObject:self];
} @end

然后在AppDelegate中粘贴以下代码并运行一次后.

//
// AppDelegate.m
// StudyCoreData
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "AppDelegate.h" #import "StoreCoordinator.h"
#import "ObjectContext.h"
#import "NSManagedObject+YX.h" #import "Student.h" @interface AppDelegate () @property (nonatomic, strong) ObjectContext *context; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"%@", NSHomeDirectory()); // 配置CoreData
[StoreCoordinator setBundleObjectModel:@"YouXianMing"
setDBPath:[self path:@"/Library/Caches/Y.Y.M.sqlite"]]; // 从coordinator获取到操作句柄
_context = [[ObjectContext alloc] initWithCoordinator:[StoreCoordinator coordinator]]; // 存储变化
[_context saveChanges]; return YES;
} - (NSString *)path:(NSString *)filePath
{
return [NSHomeDirectory() stringByAppendingPathComponent:filePath];
} @end

配置CoreData其实就是以下含义.

然后运行以下代码后查看执行结果.

//
// AppDelegate.m
// StudyCoreData
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "AppDelegate.h" #import "StoreCoordinator.h"
#import "ObjectContext.h"
#import "NSManagedObject+YX.h" #import "Student.h" @interface AppDelegate () @property (nonatomic, strong) ObjectContext *context; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 配置CoreData
[StoreCoordinator setBundleObjectModel:@"YouXianMing"
setDBPath:[self path:@"/Library/Caches/Y.Y.M.sqlite"]]; // 从coordinator获取到操作句柄
_context = [[ObjectContext alloc] initWithCoordinator:[StoreCoordinator coordinator]]; // 创建学生对象
Student *stu1 = [Student createInContext:_context];
Student *stu2 = [Student createInContext:_context];
stu1.name = @"YouXianMing";
stu2.name = @"QiuLiang"; // 存储变化
[_context saveChanges]; // 遍历出所有学生
NSArray *allStudents = [Student allInContext:_context];
for (Student *stu in allStudents)
{
NSLog(@"%@", stu.name);
} return YES;
} - (NSString *)path:(NSString *)filePath
{
return [NSHomeDirectory() stringByAppendingPathComponent:filePath];
} @end

以上就已经完成了CoreData记录的创建以及记录的遍历.

我们在试一下删除掉一个记录吧.

运行以下代码后查看执行结果.

//
// AppDelegate.m
// StudyCoreData
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "AppDelegate.h" #import "StoreCoordinator.h"
#import "ObjectContext.h"
#import "NSManagedObject+YX.h" #import "Student.h" @interface AppDelegate () @property (nonatomic, strong) ObjectContext *context; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 配置CoreData
[StoreCoordinator setBundleObjectModel:@"YouXianMing"
setDBPath:[self path:@"/Library/Caches/Y.Y.M.sqlite"]]; // 从coordinator获取到操作句柄
_context = [[ObjectContext alloc] initWithCoordinator:[StoreCoordinator coordinator]]; // 遍历出所有学生
NSArray *allStudents = [Student allInContext:_context];
for (Student *stu in allStudents)
{
if ([stu.name isEqualToString:@"QiuLiang"])
{
// 删除掉对象
[stu deleteSelf];
}
} // 存储变化
[_context saveChanges]; // 遍历出所有学生
NSArray *all = [Student allInContext:_context];
for (Student *stu in all)
{
NSLog(@"%@", stu.name);
} return YES;
} - (NSString *)path:(NSString *)filePath
{
return [NSHomeDirectory() stringByAppendingPathComponent:filePath];
} @end

查看下图,记录被删除了哦.

用起来是不是很容易呢:)

关于CoreData的查询语句,这个就需要看官自己根据需要修改我的源码了,本人只是抛砖引玉介绍CoreData的基本使用方法,剩下的就靠大家了哦.

使用CoreData [4]的更多相关文章

  1. iOS基本数据库存储方式 - CoreData

    CoreData 创建模型文件的过程 1.选择模板 2.添加实体 3.添加实体的属性[注意]属性的首字母必须小写 一.CoreData管理类(必备以下三个类对象) 1.CoreData数据操作的上下文 ...

  2. iOS CoreData 中 objectID 的不变性

    关于 CoreData的 objectID 官方文档有这样的表述:新建的Object还没保存到持久化存储上,那么它的objectID是临时id,而保存之后,就是持久化的id,不会再变化了. 那么,我想 ...

  3. CoreData __ 基本原理

    操作过程 Context想要获取值,先要告诉连接器,我要什么东西 链接器再告诉store, 你给我什么东西, store去找 找到之后返回给链接器,链接器再返回给Context          Co ...

  4. iOS CoreData primitive accessor

    Given an entity with an attribute firstName, Core Data automatically generates firstName, setFirstNa ...

  5. 初识CoreData与详解

    Core Data数据持久化是对SQLite的一个升级,它是iOS集成的,在说Core Data之前,我们先说说在CoreData中使用的几个类. (1)NSManagedObjectModel(被管 ...

  6. CoreData教程

    网上关于CoreData的教程能搜到不少,但很多都是点到即止,真正实用的部分都没有讲到,而基本不需要的地方又讲了太多,所以我打算根据我的使用情况写这么一篇实用教程.内容将包括:创建entity.创建r ...

  7. CoreData和SQLite多线程访问时的线程安全

    关于CoreData和SQLite多线程访问时的线程安全问题 数据库读取操作一般都是多线程访问的.在对数据进行读取时,我们要保证其当前状态不能被修改,即读取时加锁,否则就会出现数据错误混乱.IOS中常 ...

  8. IOS数据存储之CoreData使用优缺点

    前言: 学习了Sqlite数据之后认真思考了一下,对于已经习惯使用orm数据库的开发者或者对sql语句小白的开发者来说该如何做好数据库开发呢?这个上网搜了一下?看来总李多虑了!apple 提供了一种数 ...

  9. iOS开发之表视图爱上CoreData

    在接触到CoreData时,感觉就是苹果封装的一个ORM.CoreData负责在Model的实体和sqllite建立关联,数据模型的实体类就相当于Java中的JavaBean, 而CoreData的功 ...

  10. CoreData

    之前在学习使用SQLite时, 需要编写大量的sql语句,完成数据的增删改查,但对于不熟悉sql语句的开发人员来说,难度较大,调试程序比较困难. 由此出现CoreData框架,将sql的操作转换成为对 ...

随机推荐

  1. 自然语言处理--TF-IDF(关键词提取)

    TF-IDF算法 TF-IDF(词频-逆文档频率)算法是一种统计方法,用以评估一字词对于一个文件集或一个语料库中的其中一份文件的重要程度.字词的重要性随着它在文件中出现的次数成正比增加,但同时会随着它 ...

  2. k折交叉验证

    原理:将原始数据集划分为k个子集,将其中一个子集作为验证集,其余k-1个子集作为训练集,如此训练和验证一轮称为一次交叉验证.交叉验证重复k次,每个子集都做一次验证集,得到k个模型,加权平均k个模型的结 ...

  3. n-grama

    一.N-Gram的原理(这个词出现在句子中出现的概率) N-Gram是基于一个假设:第n个词出现与前n-1个词相关,而与其他任何词不相关.(这也是隐马尔可夫当中的假设.)整个句子出现的概率就等于各个词 ...

  4. 07-python链接mysql

    python3 中使用的是 PyMySQL模块, 取代了之前的 MysqlDB 模块, 其实使用都是一样的, 如果没有该模块的, 需要使用pip命令进行安装 pip install PyMySQL 安 ...

  5. springMVC中ModelAndView学写笔记

    api介绍: 构造函数摘要 ModelAndView()           bean样式用法的默认构造函数:填充bean属性,而不是传递构造函数参数. ModelAndView(Object vie ...

  6. Storm框架:如何消费RabbitMq消息(代码案例)

    1.定义拓扑topology public class MessageTopology { public static void main(String[] args) throws Exceptio ...

  7. Javascript实现图片翻转

    使用Js的对象属性能实现简单的翻转效果.通过onMouseover事件和onMouseOut事件就能实现鼠标移到链接上和移开链接发生反应.给两个图片加上链接实现都能翻转的效果. reverse.htm ...

  8. JAVA成员方法的调用分析

    如下面例子: public class A { int x=10; public int getx() {return x;} } public class B extends A { int x=2 ...

  9. ZooKeeper 节点

    ZNode zk 中的每一个数据节点称为 ZNode ,所有的 ZNode 按层次化结构进行组织,形成一棵树(与Linux文件系统相似).节点可以写入数据,也可以在节点下面创建子节点. 节点类型: 1 ...

  10. 后台管理UI+功能

    kingRoad功能强大,丰富各种功能和插件,自定义开发功能组件及样式,使用webpack方式集成和纯html两种,UI(自定义开发和AdminLTE的集成优化),弹窗功能使用(Layer弹窗,并与框 ...