美文网首页
realm数据开发(三)

realm数据开发(三)

作者: 小歪子go | 来源:发表于2017-11-29 14:23 被阅读0次

    刚开始做ios开发的时候本地存储选择数据库的时候常用sqlite的第三方库FMDB,后来在上家公司用过Nosql数据库,一直在寻求一个方面快捷的本地存储的方式,偶然的机会接触到了realm数据,原理性的东西我就不讲了,大家可以看以下两篇文章技术参考文档理论知识参考文档,以下让我介绍下realm数据库使用过程中需要注意的两点

    1、封装方法,避免realm自带类对代码的侵染,废话不多说,直接上代码。

    .h中的方法
    /**

    门诊检查的realm数据库中的对象

    */

    @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;
    //导医
    @property(strong,nonatomic)NSString *director_name;
    //检查日期
    @property(copy,nonatomic)NSString *record_date;
    //门诊检查的id
    @property(strong,nonatomic)NSString *examId;
    //
    @property(strong,nonatomic)NSString *examine_id;
    //图片数组的序列化后的字符串
    @property(strong,nonatomic)NSString *pics;
    //主任ID
    @property(copy,nonatomic)NSString *consult_man_id;
    //咨询师
    @property(copy,nonatomic)NSString *consult_man;
    /**已经分诊主任姓名*/
    @property(copy,nonatomic)NSString *consult_name;
    /**
     保存或者更新门诊检查到本地草稿箱
     @param model 门诊检查对象
     @param block 是否保存成功的回调
     */
    +(void)saveExameToDraftBoxWithExamModel:(KKExamModel *)model completeBlock:(void(^)(BOOL success))block;
    
    
    /**
     获取草稿箱里面的数据
     @return 返回门诊检查数组
     */
    +(NSArray<KKExamModel *> *)getExamsFromDrftBox;
    
    /**
     从草稿箱删除草稿
     @param examId 草稿箱里面examId
     @param block 删除成功与否的回调
     */
    +(void)deleteExamFromDraftWithExamId:(NSString *)examId completeBlock:(void(^)(BOOL success))block;
    

    .m文件中的实现方法

    @implementation KKExamRLMModel

    /设置主键/

    +(NSString *)primaryKey {
        return @"examId";
    }
    

    /数据保存和更新的方法/

    +(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);
        }
    }
    

    /获取数据的方法/

    +(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];
    }
    

    /删除数据的方法/

    +(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);
        }
    }
    

    普通的对象转成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对象转换成普通的对象

    /将relam对象转换成普通的对象,避免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;
    }
    

    /加这个方法为了保持代码的健壮性/

    -(void)setValue:(id)value forUndefinedKey:(NSString *)key {
        NSLog(@"unDefinekey:%@",key);
    }
    

    以上代码的重点是realm对象和普通对象之间的相互转换。
    realm封装详见realm数据库封装password:dugd

    相关文章

      网友评论

          本文标题:realm数据开发(三)

          本文链接:https://www.haomeiwen.com/subject/lpiubxtx.html