https://github.com/qxuewei/XWDatabase ? ?拿走
1.github 上搜索 ?XWDatabase 下載多的那個應(yīng)該就是了
2.好好看 人家寫的 就會用了
使用
- (void)saveOnePerson
? ? {? XWPerson *person = [XWPerson testPerson:2];
? ? ? ? [XWDatabase saveModel:person completion:^(BOOLisSuccess) {
? ? ? ? }];
? ? }
實例化一個對象, 調(diào)用?saveModel?方法。
? ? - (void)saveModels
? ? {? NSMutableArray *persons = [[NSMutableArray alloc] init];
? ? ? ? for(inti =0; i <1000; i++) {
? ? ? ? ? ? [persons addObject:[XWPerson testPerson:i]];
? ? ? ? }
? ? ? ? [XWDatabase saveModels:persons completion:^(BOOLisSuccess) {
? ? ? ? }];
? ? }
實例化一堆對象, 調(diào)用?saveModels?方法。
? ? - (void)deleteModel
? ? {
? ? ? ? XWPerson *person = [XWPerson new];
? ? ? ? person.cardID =@"1";/// 指定想刪除的主鍵(或聯(lián)合主鍵)
? ? ? ? [XWDatabase deleteModel:person completion:^(BOOLisSuccess) {
? ? ? ? }];
? ? }
實例化一個對象,為主鍵賦值(得知道刪的是哪個,讓她猜,臣妾做不到), 調(diào)用?deleteModel?方法。
2.刪除此模型存儲的所有數(shù)據(jù) (Delete All Models)
? ? - (void)clearModel ??
?{ ? ? ? ?[XWDatabase clearModel:XWPerson.class completion:^(BOOLisSuccess) { ? ? ??
?}]; ? ?}
調(diào)用?clearModel?方法,傳入想刪除的模型類
3.選擇性刪除此模型存儲的數(shù)據(jù) (Delete Models With Condition)
? ? - (void)clearModel
? ? {
? ? ? ? [XWDatabase clearModel:XWPerson.class condition:@"age > '50'"completion:^(BOOLisSuccess) {
? ? ? ? }];
? ? }
調(diào)用?clearModel?方法,傳入想刪除的模型類和條件
1.更新某模型某個成員變量 (選擇性更新) (Update some properties in Model)
? ? /// 改名
? ? - (void)updateModel
? ? {
? ? ? ? XWPerson *person = [XWPerson new];
? ? ? ? person.cardID =@"2";
? ? ? ? person.name =@"新名字";
? ? ? ? /// 自定義成員變量更新
? ? ? ? [XWDatabase updateModel:person updatePropertys:@[@"name"]completion:^(BOOLisSuccess) {
? ? ? ? }];
? ? }
實例化一個對象,為主鍵和有變化的成員變量賦值, 調(diào)用?updateModel?方法,傳入想更新的成員變量名稱。
2.更新某模型所有數(shù)據(jù) (全量更新) (Update all properties in Model)
? ? /// 根據(jù)傳入的模型整體更新
? ? - (void)updateModel
? ? {
? ? ? ? XWPerson *person = [XWPerson new];
? ? ? ? person.cardID =@"2";
? ? ? ? person.name =@"新名字";
? ? ? ? person.girls =@[@"小妹",@"校花",@"小baby"];
? ? ? ? /// 整個模型更新
? ? ? ? [XWDatabase saveModel:person completion:^(BOOLisSuccess) {
? ? ? ? }];
? ? }
實例化一個對象, 調(diào)用?updateModel?方法,傳入想更新的模型。
1.根據(jù)主鍵查詢模型 (Search One Model with primary Key)
? ? - (void)getOnePerson
? ? {
? ? ? ? XWPerson *person = [XWPerson new];
? ? ? ? person.cardID =@"81";
? ? ? ? [XWDatabase getModel:person completion:^(XWPerson * obj) {
? ? ? ? }];
? ? }
實例化一個對象,為主鍵賦值, 調(diào)用?getModel?方法。
2.查詢數(shù)據(jù)庫中所有該模型存儲的數(shù)據(jù) (Search all Models in the database)
? ? - (void)getModels
? ? {
? ? ? ? [XWDatabase getModels:XWPerson.class completion:^(NSArray *_Nullableobjs) {
? ? ? ? }];
? ? }
調(diào)用?getModels?方法,傳入模型類
? ? /// 獲取數(shù)據(jù)庫中所有該模型存儲的數(shù)據(jù) - 按 age 字段降序排列
? ? - (void)getModelsSortAge
? ? {
? ? ? ? [XWDatabase getModels:XWPerson.class sortColumn:@"age"isOrderDesc:YEScompletion:^(NSArray *_Nullableobjs) {
? ? ? ? }];
? ? }
調(diào)用?getModels?方法,傳入模型類和要排序的字段
4.查詢數(shù)據(jù)庫中所有該模型存儲的數(shù)據(jù) - 自定義查詢條件 (Search all the Models with condition)
? ? /// 獲取數(shù)據(jù)庫中所有該模型存儲的數(shù)據(jù) - 自定義查找條件 (例如模糊查詢 name 含 學偉 的數(shù)據(jù))
? ? - (void)getModelsCondition
? ? {
? ? ? ? [XWDatabase getModels:XWPerson.class condition:@"name like '%學偉'"completion:^(NSArray *_Nullableobjs) {
? ? ? ? }];
? ? }
調(diào)用?getModels?方法,傳入模型類和查詢的條件
? ? /// 獲取數(shù)據(jù)庫中所有該模型存儲的數(shù)據(jù) - 自定義查找條件可排序 (例如模糊查詢 name 含 學偉 的數(shù)據(jù), 并且按 age 升序排序)
? ? - (void)getModelsConditionSort
? ? {
? ? ? ? [XWDatabase getModels:XWPerson.class sortColumn:@"age"isOrderDesc:NOcondition:@"name like '%學偉'"completion:^(NSArray *_Nullableobjs) {
? ? ? ? }];
? ? }
調(diào)用?getModels?方法,傳入模型類和查詢的條件和排序的成員變量名稱
五、數(shù)據(jù)遷移 (Data Migration)
模型中成員變量發(fā)生變化,動態(tài)進行數(shù)據(jù)遷移
? ? + (void)initialize
? ? + {
? ? ? ? [XWDatabase updateTable:selfcompletion:^(BOOLisSuccess) {
? ? ? ? }]
在模型對象的?initialize?方法中 調(diào)用?updateTable?方法。之所以在?initialize?方法中調(diào)用是保證用戶無感知的情況下在操作此模型進行數(shù)據(jù)操作時自動更新。
以上就是?XWDatabase?V1.0 版本的所有功能示例。謝謝!
下面介紹一些使用規(guī)范和功能擴展。
六 、XWDatabaseModelProtocol?協(xié)議
?/**
?? ? 主鍵 不可更改/唯一性
?? ? @return 主鍵的屬性名
?? ? */
? ? + (NSString *)xw_primaryKey;
? ? /**
?? ? 聯(lián)合主鍵成員變量數(shù)組 (多個屬性共同定義主鍵) - 優(yōu)先級大于 'xw_primaryKey'
?? ? @return 聯(lián)合主鍵成員變量數(shù)組
?? ? */
? ? + (NSArray < NSString * > *)xw_unionPrimaryKey;
? ? /**
?? ? 自定義對象映射? (key: 成員變量名稱 value: 對象類)
?? ? @return 自定義對象映射
?? ? */
? ? + (NSDictionary *)xw_customModelMapping;
? ? /**
?? ? 忽略不保存數(shù)據(jù)庫的屬性
?? ? @return 忽略的屬性名數(shù)組
?? ? */
? ? + (NSSet *)xw_ignoreColumnNames;
? ? /**
?? ? 自定義字段名映射表 (默認成員變量即變量名, 可自定義字段名 key: 成員變量(屬性)名稱? value: 自定義數(shù)據(jù)庫表字段名)
?? ? @return 自定義字段名映射表
?? ? */
? ? + (NSDictionary *)xw_customColumnMapping;
? ? /**
?? ? 自定義表名 (默認屬性類名)
?? ? @return 自定義表名
?? ? */
?? ?+ (NSString *)xw_customTableName;