当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS App开发中Core Data框架基本的数据管理功能小结

iOS App开发中Core Data框架基本的数据管理功能小结

2019年07月24日  | 移动技术网IT编程  | 我要评论

男子扛妻遗体步行,八年级英语听力,济宁郑媛媛老师照片

一、何为coredata
coredata是一个专门用来管理数据的框架,其在性能与书写方便上都有很大的优势,在数据库管理方面,apple强烈推荐开发者使用coredata框架,在apple的官方文档中称,使用coredata框架可以减少开发者50%——70%的代码量,这虽然有些夸张,但由此可见,coredata的确十分强大。

二、设计数据模型
在ios开发中,时常使用sql数据库对大量的表结构数据进行处理,但是sql有一个十分明显的缺陷,对于常规数据模型的表,其处理起来是没问题的,例如一个班级表,其中每条数据中有班级名称,人数这样的属性,一个学生表,其中每条数据有学生的姓名,性别,年龄这样的属性。但是如果要在表与表之间建立联系,自定义对象与自定义对象之间产生从属关系,使用sql处理起来就十分麻烦了,例如如果这个班级表中有一个班长的属性,这个属性是一个学生类型。
1.创建实体类型及其属性
使用xcode创建一个工程,在工程中新建一个文件,选择core data分类中的datamodel创建,如下图:

201662393010769.png (732×519)

这时在xcode的文件导航区会出现一个以xcdatamodeld为扩展名的文件,这个文件就是数据模型文件,点击add entity按钮添加一个实体类型,取名为schoolclass,为这个类型添加两个属性,分别为名字name和学生数量stunum,如下图:

201662393043714.png (885×899)

2.对实体类型进行设置
在xcode右侧的工具栏中可以对实体类型进行一些设置,选中一个实体类型,如下图:

201662393110376.png (259×571)

name设置实体类型的名称,abstract entity设置是否是抽象实体,如果勾选,则此实体不能被实例化,只能被继承,类似于抽象类,比如定义人为一个实体类型,在定义继承于人实体类型的老师、学生等来进行实例化。parent entity用来选择父类实体,class用于设置对应的类的。

3.在实体对象之间建立关系
再创建一个学生类实体student,添加name和age两个属性。选中schoolclass,在其中的relationships模块中点击+号,来添加一个关系,如下图:

201662393139323.png (880×490)

这时,schoolclass实体类型中就有了一个student类型的班长属性。如果切换一下编辑风格,可以更加清晰的看到实体类型之间的关系,如下图:

201662393202625.png (882×871)

4.对属性和关系进行设置
选中一个属性或者关系,在右侧的工具栏中可以对属性进行一些设置,如下图:

201662393221365.png (260×544)

name设置属性的名字,optional类型代表可选,即在实例化对象时可以赋值也可以不赋值。attribute设置属性的数据类型,default value设置数据的默认值。
二、数据模型管理类nsmanagedobjectmodel

通过nsmanagedobjectmodel,可以将创建的数据模型文件读取为模型管理类对象,使用如下方法:
//获取.xcdatamodeld文件url
nsurl *modelurl = [[nsbundle mainbundle]urlforresource:@"model" withextension:@"momd"];
//读取文件
nsmanagedobjectmodel * mom = [[nsmanagedobjectmodel alloc]initwithcontentsofurl:modelurl];
其中还有一些属性和方法进行数据模型的管理:

//将多个数据模型管理文件进行合并
+ (nullable nsmanagedobjectmodel *)mergedmodelfrombundles:(nullable nsarray<nsbundle *> *)bundles; 
//将多个数据模型管理类对象进行合并
+ (nullable nsmanagedobjectmodel *)modelbymergingmodels:(nullable nsarray<nsmanagedobjectmodel *> *)models;
//存放数据中所有实体模型的字典 字典中是实体名和实体描述对象
@property (readonly, copy) nsdictionary<nsstring *, nsentitydescription *> *entitiesbyname;
//存放数据中所有实体描述对象
@property (strong) nsarray<nsentitydescription *> *entities;
//返回所有可用的配置名称
@property (readonly, strong) nsarray<nsstring *> *configurations;
//获取关联某个配置的所有实体
- (nullable nsarray<nsentitydescription *> *)entitiesforconfiguration:(nullable nsstring *)configuration;
//为某个实体关联配置
- (void)setentities:(nsarray<nsentitydescription *> *)entities forconfiguration:(nsstring *)configuration;
//创建请求模板
- (void)setfetchrequesttemplate:(nullable nsfetchrequest *)fetchrequesttemplate forname:(nsstring *)name;
//获取请求模板
- (nullable nsfetchrequest *)fetchrequesttemplateforname:(nsstring *)name;
关于实体描述对象nsentitydescription:

实体类似于数据库中的表结构,例如上次我们创建的班级实体模型,一个实体模型中可以添加许多属性与关系,nsentitydescription对象中存放这些信息,常用如下:

//实体所在的模型管理对象
@property (readonly, assign) nsmanagedobjectmodel *managedobjectmodel;
//实体所在的模型管理对象的名称
@property (null_resettable, copy) nsstring *managedobjectclassname;
//实体名
@property (nullable, copy) nsstring *name;
//设置是否是抽象实体
@property (getter=isabstract) bool abstract;
//子类实体字典
@property (readonly, copy) nsdictionary<nsstring *, nsentitydescription *> *subentitiesbyname;
//所有子类实体对象数组
@property (strong) nsarray<nsentitydescription *> *subentities;
//父类实体
@property (nullable, readonly, assign) nsentitydescription *superentity;
//所有属性字典
@property (readonly, copy) nsdictionary<nsstring *, __kindof nspropertydescription *> *propertiesbyname;
//所有属性数组
@property (strong) nsarray<__kindof nspropertydescription *> *properties;
//所有常类型属性
@property (readonly, copy) nsdictionary<nsstring *, nsattributedescription *> *attributesbyname;
//所有关系
@property (readonly, copy) nsdictionary<nsstring *, nsrelationshipdescription *> *relationshipsbyname;
//某个实体类型的所有关系
- (nsarray<nsrelationshipdescription *> *)relationshipswithdestinationentity:(nsentitydescription *)entity;
//判断是否是某种实体
- (bool)iskindofentity:(nsentitydescription *)entity;
nspropertydescription类是数据模型属性的父类,nsattributedescription和nsrelationshipdescription都是继承于nspropertydescription类,nsattributedescription描述正常类型的属性,nsrelationshipdescription用于描述自定义类型的关系。

三、持久化存储协调者类nspersistentstorecoordinator

nspersistentstorecoordinator建立数据模型与本地文件或数据库之间的联系,通过它将本地数据读入内存或者将修改过的临时数据进行持久化的保存。其初始化与链接数据持久化接收对象方法如下:

//通过数据模型管理对象进行初始化
- (instancetype)initwithmanagedobjectmodel:(nsmanagedobjectmodel *)model;
//添加一个持久化的数据接收对象
- (nullable __kindof nspersistentstore *)addpersistentstorewithtype:(nsstring *)storetype configuration:(nullable nsstring *)configuration url:(nullable nsurl *)storeurl options:(nullable nsdictionary *)options error:(nserror **)error;
//移除一个持久化的数据接收对象
- (bool)removepersistentstore:(nspersistentstore *)store error:(nserror **)error;

四、数据对象管理上下文nsmanagedobjectcontext

nsmanagedobjectcontext是进行数据管理的核心类,我们通过这个类来进行数据的增删改查等操作。其中常用方法如下:

//初始化方法 通过一个并发类型进行初始化 参数枚举如下:
/*
typedef ns_enum(nsuinteger, nsmanagedobjectcontextconcurrencytype) {
    nsprivatequeueconcurrencytype  = 0x01,//上下文对象与私有队列关联
    nsmainqueueconcurrencytype   = 0x02//上下文对象与主队列关联
};
*/
- (instancetype)initwithconcurrencytype:(nsmanagedobjectcontextconcurrencytype)ct;
//异步执行block
- (void)performblock:(void (^)())block;
//同步执行block
- (void)performblockandwait:(void (^)())block;
//关联数据持久化对象
@property (nullable, strong) nspersistentstorecoordinator *persistentstorecoordinator;
//是否有未提交的更改
@property (nonatomic, readonly) bool haschanges;
//进行查询数据请求
- (nullable nsarray *)executefetchrequest:(nsfetchrequest *)request error:(nserror **)error;
//进行查询数据条数请求
- (nsuinteger) countforfetchrequest: (nsfetchrequest *)request error: (nserror **)error ;
//插入元素
- (void)insertobject:(nsmanagedobject *)object;
//删除元素
- (void)deleteobject:(nsmanagedobject *)object;
//回滚一步操作
- (void)undo;
//清楚缓存
- (void)reset;
//还原数据
- (void)rollback;
//提交保存数据
- (bool)save:(nserror **)error;

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网