iOS 本地缓存 归档数据模型化


-(void)getHomeurl:(NSString *)url

{


    NSMutableDictionary *param = [NSMutableDictionary dictionary];


    if ([url isEqualToString:KgetHomePageV20]){



        

        [param setValue:KmemberId forKey:@"memberId"];

//        [param setValue:@"4" forKey:@"advertisementSize"];

//        [param setValue:@"3" forKey:@"recommendationSize"];

        [param setValue:@"9" forKey:@"shotSize"];

        [param setValue:KAPPID forKey:@"appId"];

        [param setValue:KappSecret forKey:@"appSecret"];

        [param setValue:[CommonTools getTimeScamp] forKey:@"timestamp"];

        

        //加密规则:sign=md5(app_id + lon+lat + time + key)

        [param setValue:[CommonTools tokenmd5StringFromArray:@[[NSString stringWithFormat:@"memberId%@",param[@"memberId"]],[NSString stringWithFormat:@"shotSize%@",param[@"shotSize"]],[NSString stringWithFormat:@"appId%@",param[@"appId"]],[NSString stringWithFormat:@"appSecret%@",param[@"appSecret"]],[NSString stringWithFormat:@"timestamp%@",param[@"timestamp"]]]] forKey:@"digest"];

        

        

    }else if ([url isEqualToString:KISUntreatedInfoV17]){

        

        NSString *requestTime;

        

        if ([[UserModel sharedInstance].oldDate length] > 0 ) {

            requestTime = [UserModel sharedInstance].oldDate;

        }else{

            requestTime = @"0";

        }

        

        [param removeAllObjects];


        

        [param setValue:KmemberId forKey:@"memberId"];

        [param setValue:requestTime forKey:@"lastDynamicId"];

        

        [param setValue:KAPPID forKey:@"appId"];

        

        [param setValue:KappSecret forKey:@"appSecret"];

        

        [param setValue:[CommonTools getTimeScamp] forKey:@"timestamp"];

        


        [param setValue:[CommonTools tokenmd5StringFromArray:@[[NSString stringWithFormat:@"memberId%@",param[@"memberId"]],[NSString stringWithFormat:@"lastDynamicId%@",param[@"lastDynamicId"]],[NSString stringWithFormat:@"appId%@",param[@"appId"]],[NSString stringWithFormat:@"appSecret%@",param[@"appSecret"]],[NSString stringWithFormat:@"timestamp%@",param[@"timestamp"]]]] forKey:@"digest"];


    }

    else{

        param = nil;

    }

    

    [HTTPRequestTool sendGetAFRequest:url withParameters:param withSuccess:^(id message) {

        

        NSDictionary *tempArr = [NSJSONSerialization JSONObjectWithData:message options:NSJSONReadingMutableContainers error:nil];

        DSLog(@"我的首页数据tempArr = %@",tempArr);

//        /**

        

        if ([url isEqualToString:KgetHomePageV20]){

            [_homeVideoArr removeAllObjects];

            [_homeDyArr removeAllObjects];

            [_homeAdArr removeAllObjects];

            

       

            

            for (NSDictionary * tempDic in tempArr[@"myShots"] ) {

                MorevideoNewModel *moreVDModel = [[MorevideoNewModel alloc]init];

                [moreVDModel setValuesForKeysWithDictionary:tempDic];

                [_homeVideoArr addObject:moreVDModel];

            }

            for (NSDictionary *temDic in tempArr[@"golfDynamics"]) {

                golfDynamicsModel *golfModel = [[golfDynamicsModel alloc]init];

                [golfModel setValuesForKeysWithDictionary:temDic];

                [_homeDyArr addObject:golfModel];

                

            }

            

            for (NSDictionary *temDic in tempArr[@"advertisements"]) {

                advertisementsModel * adModel = [[advertisementsModel alloc]init];

                [adModel setValuesForKeysWithDictionary:temDic];

                [_homeAdArr addObject:adModel];

            }

            

            NSLog(@"从网络中获取数据 ---  缓存到本地");


            [[JXTCacher cacher] setObject:_homeVideoArr // save data to local disk and cache

                       forKey:@"myShots"

                       userId:@"myShots01"

                   useArchive:YES

                       setted:^(JXTCacher *cacher, CacheError error) {

                       }];


            [[JXTCacher cacher] setObject:_homeDyArr // save data to local disk and cache

                       forKey:@"golfDynamics"

                       userId:@"golfDynamics01"

                   useArchive:YES

                       setted:^(JXTCacher *cacher, CacheError error) {

                           

                       }];

           

            [[JXTCacher cacher] setObject:_homeAdArr // save data to local disk and cache

                       forKey:@"advertisements"

                       userId:@"advertisements01"

                   useArchive:YES

                       setted:^(JXTCacher *cacher, CacheError error) {

                           

                       }];


            

            [self.homeCollectionView reloadData];

            

        }else if ([url isEqualToString:KISUntreatedInfoV17]){

            

            DSLog(@"----------returnCode--------%@",tempArr[@"returnCode"]);

            

            if ([tempArr[@"returnCode"] isEqualToString:@"29"] || [tempArr[@"returnCode"] isEqualToString:@"31"]) {

                [[NSNotificationCenter defaultCenter]postNotificationName:@"TokenFail" object:nil];

            }


            //  NSString *oldDateStr = [messageBody.regDate stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

            // [UserModel sharedInstance].oldDate = oldDateStr;

            ISUntreatedInfoModel *isModel = [[ISUntreatedInfoModel alloc]init];

            [isModel setValuesForKeysWithDictionary:tempArr];

            

            

            //通讯录

            if (isModel.ISUntreatedFriendInfo > 0) {

                

                [UserModel sharedInstance].ISUntreatedFriendInfo = [NSString stringWithFormat:@"%d",isModel.ISUntreatedFriendInfo];

                

                UIViewController *cvc = [[UIViewController alloc]init];

                cvc.view.tag = 4;

                NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:cvc,@"RedDot", nil];

                NSNotification *notification = [NSNotification notificationWithName:@"RedDot" object:nil userInfo:dict];

                [[NSNotificationCenter defaultCenter]postNotification:notification];

                

            }else{

                UIViewController *cvc = [[UIViewController alloc]init];

                cvc.view.tag = 3;

                NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:cvc,@"RedDot", nil];

                NSNotification *notification = [NSNotification notificationWithName:@"RedDot" object:nil userInfo:dict];

                [[NSNotificationCenter defaultCenter]postNotification:notification];

            

            }

            

            

            

            //动态

            if (isModel.ISNewDynamic > 0) {

                UIViewController *cvc = [[UIViewController alloc]init];

                cvc.view.tag = 1;

                NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:cvc,@"RedDot", nil];

                NSNotification *notification = [NSNotification notificationWithName:@"RedDot" object:nil userInfo:dict];

                [[NSNotificationCenter defaultCenter]postNotification:notification];

            }else{

                UIViewController *cvc = [[UIViewController alloc]init];

                cvc.view.tag = 0;

                NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:cvc,@"RedDot", nil];

                NSNotification *notification = [NSNotification notificationWithName:@"RedDot" object:nil userInfo:dict];

                [[NSNotificationCenter defaultCenter]postNotification:notification];

            }

            

    }

        

        

        

        

       

        

    } andWithFail:^(id message) {

        NSLog(@"error = %@",message);

    }];

}




-(void)loadData

{

    

    [[JXTCacher cacher] objectForKey:@"myShots" userId:@"myShots01" achive:^(JXTCacher *cacher, id obj, CacheError error) {

        

        dispatch_async(dispatch_get_main_queue(), ^{

            

            if (obj && error == 0) {  //local or cache have data

                _homeVideoArr = obj;

                NSLog(@"从缓存中获取数据");

                

                [self.homeCollectionView reloadData];

                

            }

            else { // local and cache have no data

                

                [self getHomeurl:KGetHomePage];

                

                NSLog(@"从网络中获取数据");

            }

            

            

        });

    }];

    

    [[JXTCacher cacher] objectForKey:@"golfDynamics" userId:@"golfDynamics01" achive:^(JXTCacher *cacher, id obj, CacheError error) {

        dispatch_async(dispatch_get_main_queue(), ^{

            

            if (obj && error == 0) {  //local or cache have data

                _homeDyArr = obj;

                NSLog(@"从缓存中获取数据");

                

                [self.homeCollectionView reloadData];

                

            }

           

            

            

        });

    }];


    [[JXTCacher cacher] objectForKey:@"advertisements" userId:@"advertisements01" achive:^(JXTCacher *cacher, id obj, CacheError error) {

        dispatch_async(dispatch_get_main_queue(), ^{

            

            

            

            if (obj && error == 0) {  //local or cache have data

                _homeAdArr = obj;

                NSLog(@"从缓存中获取数据");

                

                [self.homeCollectionView reloadData];

                

            }

           

            

            

        });

    }];


}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值