在上一篇博客中,介绍了iOS中使用CoreData框架设计数据模型的相关步骤。CoreData框架中经过相关的类将数据——数据模型——开发者无缝的衔接起来。NSManagedObjectModel对应数据模型,即上篇博客中咱们建立的.xcdatamodeld文件;NSPersistentStoreCoordinator至关于数据库与数据模型之间的桥接器,经过NSPersistentStoreCoordinator将数据模型存入数据库;NSManagedObjectContext是核心的数据管理类,开发者经过操做它来执行对数据的相关操做。数据库
经过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创建数据模型与本地文件或数据库之间的联系,经过它将本地数据读入内存或者将修改过的临时数据进行持久化的保存。其初始化与连接数据持久化接收对象方法以下:fetch
//经过数据模型管理对象进行初始化 - (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是进行数据管理的核心类,咱们经过这个类来进行数据的增删改查等操做。其中经常使用方法以下:atom
//初始化方法 经过一个并发类型进行初始化 参数枚举以下: /* 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;
专一技术,热爱生活,交流技术,也作朋友。url
——珲少 QQ群:203317592spa