剛開始做ios開發(fā)的時(shí)候本地存儲選擇數(shù)據(jù)庫的時(shí)候常用sqlite的第三方庫FMDB,后來在上家公司用過Nosql數(shù)據(jù)庫,一直在尋求一個(gè)方面快捷的本地存儲的方式,偶然的機(jī)會接觸到了realm數(shù)據(jù),原理性的東西我就不講了钥弯,大家可以看以下兩篇文章技術(shù)參考文檔盗蟆,理論知識參考文檔虫埂,以下讓我介紹下realm數(shù)據(jù)庫使用過程中需要注意的兩點(diǎn)
1贷盲、封裝方法坏匪,避免realm自帶類對代碼的侵染拟逮,廢話不多說,直接上代碼适滓。
.h中的方法
/**
門診檢查的realm數(shù)據(jù)庫中的對象
*/
@interface KKExamRLMModel : KKRLMBaseObject
//牙圖的類
@property(strong,nonatomic)NSNumber<RLMInt> *tooth_type;
//客戶id
@property(copy,nonatomic)NSString *client_id;
//客戶姓名
@property(copy,nonatomic)NSString *name;
//前臺接待ID
@property(copy,nonatomic)NSString *receive_man_id;
//前臺接待
@property(copy,nonatomic)NSString *receive_man;
//其它口腔問題
@property(copy,nonatomic)NSString *remark;
//牙位圖列表
@property(strong,nonatomic)NSString *tooth_list;
//導(dǎo)醫(yī)
@property(strong,nonatomic)NSString *director_name;
//檢查日期
@property(copy,nonatomic)NSString *record_date;
//門診檢查的id
@property(strong,nonatomic)NSString *examId;
//
@property(strong,nonatomic)NSString *examine_id;
//圖片數(shù)組的序列化后的字符串
@property(strong,nonatomic)NSString *pics;
//主任ID
@property(copy,nonatomic)NSString *consult_man_id;
//咨詢師
@property(copy,nonatomic)NSString *consult_man;
/**已經(jīng)分診主任姓名*/
@property(copy,nonatomic)NSString *consult_name;
/**
保存或者更新門診檢查到本地草稿箱
@param model 門診檢查對象
@param block 是否保存成功的回調(diào)
*/
+(void)saveExameToDraftBoxWithExamModel:(KKExamModel *)model completeBlock:(void(^)(BOOL success))block;
/**
獲取草稿箱里面的數(shù)據(jù)
@return 返回門診檢查數(shù)組
*/
+(NSArray<KKExamModel *> *)getExamsFromDrftBox;
/**
從草稿箱刪除草稿
@param examId 草稿箱里面examId
@param block 刪除成功與否的回調(diào)
*/
+(void)deleteExamFromDraftWithExamId:(NSString *)examId completeBlock:(void(^)(BOOL success))block;
.m文件中的實(shí)現(xiàn)方法
@implementation KKExamRLMModel
/設(shè)置主鍵/
+(NSString *)primaryKey {
return @"examId";
}
/數(shù)據(jù)保存和更新的方法/
+(void)saveExameToDraftBoxWithExamModel:(KKExamModel *)model completeBlock:(void(^)(BOOL success))block{
KKExamRLMModel *rlmModel=[KKExamRLMModel convertToRLMExamModel:model];
[KKExamRLMModel addorUpdateWithObject:rlmModel afterBlock:^{
}];
RLMResults *results=[KKExamRLMModel getObjetctsWithWhere:[NSString stringWithFormat:@"examId='%@'",model.examId] sortedDescriptors:nil];
if (results.count==0) {
block(NO);
}
else {
block(YES);
}
}
/獲取數(shù)據(jù)的方法/
+(NSArray<KKExamModel *> *)getExamsFromDrftBox {
RLMSortDescriptor *descriptor=[RLMSortDescriptor sortDescriptorWithKeyPath:@"record_date" ascending:NO];
NSMutableArray<KKExamModel *> *array=[NSMutableArray arrayWithCapacity:10];
RLMResults *rlmArray=[KKExamRLMModel getAllObjectsWithSortedDescriptors:@[descriptor]];
for (KKExamRLMModel *rlmModel in rlmArray) {
KKExamModel *examModel=[KKExamRLMModel convertToExamModel:rlmModel];
[array addObject:examModel];
}
return [array copy];
}
/刪除數(shù)據(jù)的方法/
+(void)deleteExamFromDraftWithExamId:(NSString *)examId completeBlock:(void(^)(BOOL success))block{
[KKExamRLMModel deleteObjectsWithWhere:[NSString stringWithFormat:@"examId='%@'",examId]];
RLMResults *results=[KKExamRLMModel getObjetctsWithWhere:[NSString stringWithFormat:@"examId='%@'",examId] sortedDescriptors:nil];
if (results.count==0) {
block(YES);
}
else {
block(NO);
}
}
普通的對象轉(zhuǎn)成realm對象
+(KKExamRLMModel *)convertToRLMExamModel:(KKExamModel *)model {
KKExamRLMModel *rlmModel=[KKExamRLMModel alloc].init;
unsigned int propertyCount = 0;
objc_property_t *propertys = class_copyPropertyList([model class], &propertyCount);
for (int i = 0; i < propertyCount; i ++) {
objc_property_t property = propertys[i];
const char * propertyName = property_getName(property);
NSString *propertyStr=[NSString stringWithUTF8String:propertyName];
if ([propertyStr isEqualToString:@"tooth_list"]) {
rlmModel.tooth_list=[KKToothMapModel getDictsFromObjects:model.tooth_list].mj_JSONString;
}
else if ([propertyStr isEqualToString:@"pics"]&&[model.pics isKindOfClass:[NSMutableArray class]]){
if (model.pics.count>1) {
NSMutableArray *mutArray=[NSMutableArray arrayWithCapacity:9];
for (int i=0; i<model.pics.count; i++) {
UIImage *tempImage=model.pics[i];
NSData *data = UIImageJPEGRepresentation(tempImage,0.5);
NSString *imageDataString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
[mutArray addObject:imageDataString];
}
[rlmModel setValue:[KKJSON arrayToJson:mutArray] forKey:propertyStr];
}
else {
[rlmModel setValue:nil forKey:propertyStr];
}
}
else {
[rlmModel setValue:[model valueForKey:propertyStr] forKey:propertyStr];
}
}
free(propertys);
return rlmModel;
}
將realm對象轉(zhuǎn)換成普通的對象
/將relam對象轉(zhuǎn)換成普通的對象敦迄,避免reaml對象對本對象的侵染/
+(KKExamModel *)convertToExamModel:(KKExamRLMModel *)rlmModel {
KKExamModel *examModel=[KKExamModel alloc].init;
unsigned int propertyCount = 0;
objc_property_t *propertys = class_copyPropertyList([KKExamRLMModel class], &propertyCount);
for (int i = 0; i < propertyCount; i ++) {
objc_property_t property = propertys[i];
const char * propertyName = property_getName(property);
NSString *propertyStr=[NSString stringWithUTF8String:propertyName];
if ([propertyStr isEqualToString:@"tooth_list"]) {
examModel.tooth_list=[[KKToothMapModel getObjectArrayWithKeyValuesArray:rlmModel.tooth_list.mj_JSONObject] mutableCopy];
}
else if ([propertyStr isEqualToString:@"pics"]) {
NSArray *temarray=[KKJSON arrayFromJsonString:rlmModel.pics];
for (NSString *str in temarray) {
NSData *decodedImgData = [[NSData alloc] initWithBase64EncodedString:str options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *decodedImage = [UIImage imageWithData:decodedImgData];
[examModel.pics addObject:decodedImage];
}
}
else {
[examModel setValue:[rlmModel valueForKeyPath:propertyStr] forKey:propertyStr];
}
}
free(propertys);
return examModel;
}
/加這個(gè)方法為了保持代碼的健壯性/
-(void)setValue:(id)value forUndefinedKey:(NSString *)key {
NSLog(@"unDefinekey:%@",key);
}
以上代碼的重點(diǎn)是realm對象和普通對象之間的相互轉(zhuǎn)換。
realm封裝詳見realm數(shù)據(jù)庫封裝password:dugd