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

来源:互联网 发布:php apache forbidden 编辑:程序博客网 时间:2024/06/06 02:59


-(void)getHomeurl:(NSString *)url

{


    NSMutableDictionary *param = [NSMutableDictionarydictionary];


    if ([urlisEqualToString:KgetHomePageV20]){



        

        [param setValue:KmemberIdforKey:@"memberId"];

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

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

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

        [param setValue:KAPPIDforKey:@"appId"];

        [param setValue:KappSecretforKey:@"appSecret"];

        [param setValue:[CommonToolsgetTimeScamp] forKey:@"timestamp"];

        

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

        [param setValue:[CommonToolstokenmd5StringFromArray:@[[NSStringstringWithFormat:@"memberId%@",param[@"memberId"]],[NSStringstringWithFormat:@"shotSize%@",param[@"shotSize"]],[NSStringstringWithFormat:@"appId%@",param[@"appId"]],[NSStringstringWithFormat:@"appSecret%@",param[@"appSecret"]],[NSStringstringWithFormat:@"timestamp%@",param[@"timestamp"]]]]forKey:@"digest"];

        

        

    }elseif ([url isEqualToString:KISUntreatedInfoV17]){

        

        NSString *requestTime;

        

        if ([[UserModelsharedInstance].oldDatelength] > 0 ) {

            requestTime = [UserModelsharedInstance].oldDate;

        }else{

            requestTime = @"0";

        }

        

        [param removeAllObjects];


        

        [param setValue:KmemberIdforKey:@"memberId"];

        [param setValue:requestTimeforKey:@"lastDynamicId"];

        

        [param setValue:KAPPIDforKey:@"appId"];

        

        [param setValue:KappSecretforKey:@"appSecret"];

        

        [param setValue:[CommonToolsgetTimeScamp] forKey:@"timestamp"];

        


        [param setValue:[CommonToolstokenmd5StringFromArray:@[[NSStringstringWithFormat:@"memberId%@",param[@"memberId"]],[NSStringstringWithFormat:@"lastDynamicId%@",param[@"lastDynamicId"]],[NSStringstringWithFormat:@"appId%@",param[@"appId"]],[NSStringstringWithFormat:@"appSecret%@",param[@"appSecret"]],[NSStringstringWithFormat:@"timestamp%@",param[@"timestamp"]]]]forKey:@"digest"];


    }

    else{

        param = nil;

    }

    

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

        

        NSDictionary *tempArr = [NSJSONSerializationJSONObjectWithData:messageoptions:NSJSONReadingMutableContainerserror:nil];

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

//        /**

        

        if ([urlisEqualToString:KgetHomePageV20]){

            [_homeVideoArrremoveAllObjects];

            [_homeDyArrremoveAllObjects];

            [_homeAdArrremoveAllObjects];

            

       

            

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

                MorevideoNewModel *moreVDModel = [[MorevideoNewModelalloc]init];

                [moreVDModel setValuesForKeysWithDictionary:tempDic];

                [_homeVideoArraddObject:moreVDModel];

            }

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

                golfDynamicsModel *golfModel = [[golfDynamicsModelalloc]init];

                [golfModel setValuesForKeysWithDictionary:temDic];

                [_homeDyArraddObject:golfModel];

                

            }

            

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

                advertisementsModel * adModel = [[advertisementsModelalloc]init];

                [adModel setValuesForKeysWithDictionary:temDic];

                [_homeAdArraddObject:adModel];

            }

            

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


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

                       forKey:@"myShots"

                       userId:@"myShots01"

                   useArchive:YES

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

                       }];


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

                       forKey:@"golfDynamics"

                       userId:@"golfDynamics01"

                   useArchive:YES

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

                           

                       }];

           

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

                       forKey:@"advertisements"

                       userId:@"advertisements01"

                   useArchive:YES

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

                           

                       }];


            

            [self.homeCollectionViewreloadData];

            

        }elseif ([url isEqualToString:KISUntreatedInfoV17]){

            

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

            

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

                [[NSNotificationCenterdefaultCenter]postNotificationName:@"TokenFail"object:nil];

            }


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

            // [UserModel sharedInstance].oldDate = oldDateStr;

            ISUntreatedInfoModel *isModel = [[ISUntreatedInfoModelalloc]init];

            [isModel setValuesForKeysWithDictionary:tempArr];

            

            

            //通讯录

            if (isModel.ISUntreatedFriendInfo >0) {

                

                [UserModelsharedInstance].ISUntreatedFriendInfo = [NSStringstringWithFormat:@"%d",isModel.ISUntreatedFriendInfo];

                

                UIViewController *cvc = [[UIViewControlleralloc]init];

                cvc.view.tag =4;

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

                NSNotification *notification = [NSNotificationnotificationWithName:@"RedDot"object:niluserInfo:dict];

                [[NSNotificationCenterdefaultCenter]postNotification:notification];

                

            }else{

                UIViewController *cvc = [[UIViewControlleralloc]init];

                cvc.view.tag =3;

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

                NSNotification *notification = [NSNotificationnotificationWithName:@"RedDot"object:niluserInfo:dict];

                [[NSNotificationCenterdefaultCenter]postNotification:notification];

            

            }

            

            

            

            //动态

            if (isModel.ISNewDynamic >0) {

                UIViewController *cvc = [[UIViewControlleralloc]init];

                cvc.view.tag =1;

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

                NSNotification *notification = [NSNotificationnotificationWithName:@"RedDot"object:niluserInfo:dict];

                [[NSNotificationCenterdefaultCenter]postNotification:notification];

            }else{

                UIViewController *cvc = [[UIViewControlleralloc]init];

                cvc.view.tag =0;

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

                NSNotification *notification = [NSNotificationnotificationWithName:@"RedDot"object:niluserInfo:dict];

                [[NSNotificationCenterdefaultCenter]postNotification:notification];

            }

            

    }

        

        

        

        

       

        

    } andWithFail:^(id message) {

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

    }];

}




-(void)loadData

{

    

    [[JXTCachercacher] 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.homeCollectionViewreloadData];

                

            }

            else {// local and cache have no data

                

                [selfgetHomeurl:KGetHomePage];

                

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

            }

            

            

        });

    }];

    

    [[JXTCachercacher] 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.homeCollectionViewreloadData];

                

            }

           

            

            

        });

    }];


    [[JXTCachercacher] 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.homeCollectionViewreloadData];

                

            }

           

            

            

        });

    }];


}





1 0
原创粉丝点击