亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS App開發(fā)中Core Data框架基本的數(shù)據(jù)管理功能小結(jié)

 更新時間:2016年06月23日 09:37:59   作者:琿少  
除了使用SQL關(guān)系型數(shù)據(jù)庫,我們還可以使用Xcode中提供的Core Data來進(jìn)行表結(jié)構(gòu)數(shù)據(jù)處理,這里我們就來初步整理iOS App開發(fā)中Core Data框架基本的數(shù)據(jù)管理功能小結(jié):

一、何為CoreData
CoreData是一個專門用來管理數(shù)據(jù)的框架,其在性能與書寫方便上都有很大的優(yōu)勢,在數(shù)據(jù)庫管理方面,apple強(qiáng)烈推薦開發(fā)者使用CoreData框架,在apple的官方文檔中稱,使用CoreData框架可以減少開發(fā)者50%——70%的代碼量,這雖然有些夸張,但由此可見,CoreData的確十分強(qiáng)大。

二、設(shè)計(jì)數(shù)據(jù)模型
在iOS開發(fā)中,時常使用SQL數(shù)據(jù)庫對大量的表結(jié)構(gòu)數(shù)據(jù)進(jìn)行處理,但是SQL有一個十分明顯的缺陷,對于常規(guī)數(shù)據(jù)模型的表,其處理起來是沒問題的,例如一個班級表,其中每條數(shù)據(jù)中有班級名稱,人數(shù)這樣的屬性,一個學(xué)生表,其中每條數(shù)據(jù)有學(xué)生的姓名,性別,年齡這樣的屬性。但是如果要在表與表之間建立聯(lián)系,自定義對象與自定義對象之間產(chǎn)生從屬關(guān)系,使用SQL處理起來就十分麻煩了,例如如果這個班級表中有一個班長的屬性,這個屬性是一個學(xué)生類型。
1.創(chuàng)建實(shí)體類型及其屬性
使用Xcode創(chuàng)建一個工程,在工程中新建一個文件,選擇Core Data分類中的DataModel創(chuàng)建,如下圖:

201662393010769.png (732×519)

這時在Xcode的文件導(dǎo)航區(qū)會出現(xiàn)一個以xcdatamodeld為擴(kuò)展名的文件,這個文件就是數(shù)據(jù)模型文件,點(diǎn)擊Add Entity按鈕添加一個實(shí)體類型,取名為SchoolClass,為這個類型添加兩個屬性,分別為名字name和學(xué)生數(shù)量stuNum,如下圖:

201662393043714.png (885×899)

2.對實(shí)體類型進(jìn)行設(shè)置
在Xcode右側(cè)的工具欄中可以對實(shí)體類型進(jìn)行一些設(shè)置,選中一個實(shí)體類型,如下圖:

201662393110376.png (259×571)

Name設(shè)置實(shí)體類型的名稱,Abstract Entity設(shè)置是否是抽象實(shí)體,如果勾選,則此實(shí)體不能被實(shí)例化,只能被繼承,類似于抽象類,比如定義人為一個實(shí)體類型,在定義繼承于人實(shí)體類型的老師、學(xué)生等來進(jìn)行實(shí)例化。Parent Entity用來選擇父類實(shí)體,Class用于設(shè)置對應(yīng)的類的。

3.在實(shí)體對象之間建立關(guān)系
再創(chuàng)建一個學(xué)生類實(shí)體Student,添加name和age兩個屬性。選中SchoolClass,在其中的Relationships模塊中點(diǎn)擊+號,來添加一個關(guān)系,如下圖:

201662393139323.png (880×490)

這時,SchoolClass實(shí)體類型中就有了一個Student類型的班長屬性。如果切換一下編輯風(fēng)格,可以更加清晰的看到實(shí)體類型之間的關(guān)系,如下圖:

201662393202625.png (882×871)

4.對屬性和關(guān)系進(jìn)行設(shè)置
選中一個屬性或者關(guān)系,在右側(cè)的工具欄中可以對屬性進(jìn)行一些設(shè)置,如下圖:

201662393221365.png (260×544)

name設(shè)置屬性的名字,Optional類型代表可選,即在實(shí)例化對象時可以賦值也可以不賦值。Attribute設(shè)置屬性的數(shù)據(jù)類型,Default Value設(shè)置數(shù)據(jù)的默認(rèn)值。
二、數(shù)據(jù)模型管理類NSManagedObjectModel

通過NSManagedObjectModel,可以將創(chuàng)建的數(shù)據(jù)模型文件讀取為模型管理類對象,使用如下方法:
//獲取.xcdatamodeld文件url
NSURL *modelUrl = [[NSBundle mainBundle]URLForResource:@"Model" withExtension:@"momd"];
//讀取文件
NSManagedObjectModel * mom = [[NSManagedObjectModel alloc]initWithContentsOfURL:modelUrl];
其中還有一些屬性和方法進(jìn)行數(shù)據(jù)模型的管理:

//將多個數(shù)據(jù)模型管理文件進(jìn)行合并
+ (nullable NSManagedObjectModel *)mergedModelFromBundles:(nullable NSArray<NSBundle *> *)bundles; 
//將多個數(shù)據(jù)模型管理類對象進(jìn)行合并
+ (nullable NSManagedObjectModel *)modelByMergingModels:(nullable NSArray<NSManagedObjectModel *> *)models;
//存放數(shù)據(jù)中所有實(shí)體模型的字典 字典中是實(shí)體名和實(shí)體描述對象
@property (readonly, copy) NSDictionary<NSString *, NSEntityDescription *> *entitiesByName;
//存放數(shù)據(jù)中所有實(shí)體描述對象
@property (strong) NSArray<NSEntityDescription *> *entities;
//返回所有可用的配置名稱
@property (readonly, strong) NSArray<NSString *> *configurations;
//獲取關(guān)聯(lián)某個配置的所有實(shí)體
- (nullable NSArray<NSEntityDescription *> *)entitiesForConfiguration:(nullable NSString *)configuration;
//為某個實(shí)體關(guān)聯(lián)配置
- (void)setEntities:(NSArray<NSEntityDescription *> *)entities forConfiguration:(NSString *)configuration;
//創(chuàng)建請求模板
- (void)setFetchRequestTemplate:(nullable NSFetchRequest *)fetchRequestTemplate forName:(NSString *)name;
//獲取請求模板
- (nullable NSFetchRequest *)fetchRequestTemplateForName:(NSString *)name;
關(guān)于實(shí)體描述對象NSEntityDescription:

實(shí)體類似于數(shù)據(jù)庫中的表結(jié)構(gòu),例如上次我們創(chuàng)建的班級實(shí)體模型,一個實(shí)體模型中可以添加許多屬性與關(guān)系,NSEntityDescription對象中存放這些信息,常用如下:

//實(shí)體所在的模型管理對象
@property (readonly, assign) NSManagedObjectModel *managedObjectModel;
//實(shí)體所在的模型管理對象的名稱
@property (null_resettable, copy) NSString *managedObjectClassName;
//實(shí)體名
@property (nullable, copy) NSString *name;
//設(shè)置是否是抽象實(shí)體
@property (getter=isAbstract) BOOL abstract;
//子類實(shí)體字典
@property (readonly, copy) NSDictionary<NSString *, NSEntityDescription *> *subentitiesByName;
//所有子類實(shí)體對象數(shù)組
@property (strong) NSArray<NSEntityDescription *> *subentities;
//父類實(shí)體
@property (nullable, readonly, assign) NSEntityDescription *superentity;
//所有屬性字典
@property (readonly, copy) NSDictionary<NSString *, __kindof NSPropertyDescription *> *propertiesByName;
//所有屬性數(shù)組
@property (strong) NSArray<__kindof NSPropertyDescription *> *properties;
//所有常類型屬性
@property (readonly, copy) NSDictionary<NSString *, NSAttributeDescription *> *attributesByName;
//所有關(guān)系
@property (readonly, copy) NSDictionary<NSString *, NSRelationshipDescription *> *relationshipsByName;
//某個實(shí)體類型的所有關(guān)系
- (NSArray<NSRelationshipDescription *> *)relationshipsWithDestinationEntity:(NSEntityDescription *)entity;
//判斷是否是某種實(shí)體
- (BOOL)isKindOfEntity:(NSEntityDescription *)entity;
NSPropertyDescription類是數(shù)據(jù)模型屬性的父類,NSAttributeDescription和NSRelationshipDescription都是繼承于NSPropertyDescription類,NSAttributeDescription描述正常類型的屬性,NSRelationshipDescription用于描述自定義類型的關(guān)系。

三、持久化存儲協(xié)調(diào)者類NSPersistentStoreCoordinator

NSPersistentStoreCoordinator建立數(shù)據(jù)模型與本地文件或數(shù)據(jù)庫之間的聯(lián)系,通過它將本地?cái)?shù)據(jù)讀入內(nèi)存或者將修改過的臨時數(shù)據(jù)進(jìn)行持久化的保存。其初始化與鏈接數(shù)據(jù)持久化接收對象方法如下:

//通過數(shù)據(jù)模型管理對象進(jìn)行初始化
- (instancetype)initWithManagedObjectModel:(NSManagedObjectModel *)model;
//添加一個持久化的數(shù)據(jù)接收對象
- (nullable __kindof NSPersistentStore *)addPersistentStoreWithType:(NSString *)storeType configuration:(nullable NSString *)configuration URL:(nullable NSURL *)storeURL options:(nullable NSDictionary *)options error:(NSError **)error;
//移除一個持久化的數(shù)據(jù)接收對象
- (BOOL)removePersistentStore:(NSPersistentStore *)store error:(NSError **)error;

四、數(shù)據(jù)對象管理上下文NSManagedObjectContext

NSManagedObjectContext是進(jìn)行數(shù)據(jù)管理的核心類,我們通過這個類來進(jìn)行數(shù)據(jù)的增刪改查等操作。其中常用方法如下:

//初始化方法 通過一個并發(fā)類型進(jìn)行初始化 參數(shù)枚舉如下:
/*
typedef NS_ENUM(NSUInteger, NSManagedObjectContextConcurrencyType) {
    NSPrivateQueueConcurrencyType  = 0x01,//上下文對象與私有隊(duì)列關(guān)聯(lián)
    NSMainQueueConcurrencyType   = 0x02//上下文對象與主隊(duì)列關(guān)聯(lián)
};
*/
- (instancetype)initWithConcurrencyType:(NSManagedObjectContextConcurrencyType)ct;
//異步執(zhí)行block
- (void)performBlock:(void (^)())block;
//同步執(zhí)行block
- (void)performBlockAndWait:(void (^)())block;
//關(guān)聯(lián)數(shù)據(jù)持久化對象
@property (nullable, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator;
//是否有未提交的更改
@property (nonatomic, readonly) BOOL hasChanges;
//進(jìn)行查詢數(shù)據(jù)請求
- (nullable NSArray *)executeFetchRequest:(NSFetchRequest *)request error:(NSError **)error;
//進(jìn)行查詢數(shù)據(jù)條數(shù)請求
- (NSUInteger) countForFetchRequest: (NSFetchRequest *)request error: (NSError **)error ;
//插入元素
- (void)insertObject:(NSManagedObject *)object;
//刪除元素
- (void)deleteObject:(NSManagedObject *)object;
//回滾一步操作
- (void)undo;
//清楚緩存
- (void)reset;
//還原數(shù)據(jù)
- (void)rollback;
//提交保存數(shù)據(jù)
- (BOOL)save:(NSError **)error;

相關(guān)文章

最新評論