省市区三级联动后台接口调用与数据预处理

网友投稿 1056 2022-11-27

省市区三级联动后台接口调用与数据预处理

省市区三级联动后台接口调用与数据预处理

由于现在经常出现撤县建市,裁撤乡村,大城市把撤县设区以及区划调整,所以需要更新省市区的数据。 解决办法是后台从民政部获取官方省市区数据,录入数据库。后台增加接口让前端调用,获取实时省市区实时数据。 由于省市区数据超过4000条记录,数据量极大,这种实时获取的方式十分费时间和流量。所以要做本地化处理。 把省市区版本号存入系统plist文件([[NSUserDefaults standardUserDefaults] objectForKey:key]),应用启动时,判断版本号是否一致,若不一致清除本地数据,若一致加载本地化数据放入内存,以便于有获取省市区。 在使用的页面判断内存中是否有省市区数据,若没有就调用省市区接口并本地化数据。 当然你若认为把省市区本地化数据加载如内存浪费内存,也使用时获取就可以。一般的手机内存都很大,也不在乎那几百k的内存。 我们需要对获取的后台三级链表数据进行排序和对象化后进行序列化。应用启动时进行反序列化。 接口实现我们采用了ReactiveCocoa (= 2.5)和MJExtension (3.0.17)共有组件。 CBPAddressManagerViewModel.m

.region_listCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) { return [RACSignal createSignal:^RACDisposable *(id subscriber) { @strongify(self); CBPAddressManagerAddEntity *entity = input; if((entity == nil) || (![entity isKindOfClass:[CBPAddressManagerAddEntity class]])) { [[BITNoticeView currentNotice] showErrorNotice:@"参数错误"]; [subscriber sendError:nil]; return nil; } self.model = entity;// entity.list_row = 20; NSMutableDictionary *dic = [NSMutableDictionary dictionary];// [dic setSafeObject:self.model.address_id forKey:@"address_id"]; self.region_listInfoApi.setParams(dic); NSLog(@"____________________%@",dic); [[BITRequestClient sharedClient] loadDataWithApi:self.region_listInfoApi successBlock:^(NSDictionary *result) { @strongify(self); if(!isCommonUnitEmptyDict(result)) { NSArray *array = result[@"data"]; if(!isCommonUnitEmptyArray(array)) { [self.model.addressDicArr removeAllObjects]; for(NSDictionary *dic1 in array) { [self.model.addressDicArr addSafeObject:dic1]; } [self processAddress]; } } [subscriber sendNext:self.model]; [subscriber sendCompleted]; } failureBlock:^(NSError *error) { [[BITNoticeView currentNotice] showErrorNotice:error.domain]; [subscriber sendError:error]; }]; return nil; }]; }];

// 加载地址信息- (void)processAddress{ NSArray*sortedArray = [self.model.addressDicArr sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1,NSDictionary *obj2) { return [obj1[@"code"] compare:obj2[@"code"] options:NSNumericSearch];//正序 }]; [[PPSingleObject sharedInstance].addressArr removeAllObjects]; for(NSDictionary *dic1 in sortedArray) { if(!isCommonUnitEmptyDict(dic1)) { BGChinaAddressEntity *entity1 = [BGChinaAddressEntity mj_objectWithKeyValues:dic1]; [self.model.addressArr addSafeObject:entity1]; NSMutableArray *sub1 = dic1[@"sub"]; if(!isCommonUnitEmptyArray(sub1)) { if(isCommonUnitEmptyString(entity1.pcode)) { entity1.subFilePath = [NSString stringWithFormat:@"%@-%@", entity1.name, entity1.code]; } else { entity1.subFilePath = [NSString stringWithFormat:@"%@-%@-%@", entity1.name, entity1.code,entity1.pcode]; } NSMutableArray *sub3 = [NSMutableArray array]; for(NSDictionary *dic2 in sub1) { BGChinaAddressEntity *entity2 = [BGChinaAddressEntity mj_objectWithKeyValues:dic2]; [sub3 addSafeObject:entity2]; if(!isCommonUnitEmptyDict(dic2)) { NSMutableArray *sub2 = dic2[@"sub"]; if(!isCommonUnitEmptyArray(sub2)) { if(isCommonUnitEmptyString(entity2.pcode)) { entity2.subFilePath = [NSString stringWithFormat:@"%@-%@", entity2.name, entity2.code]; } else { entity2.subFilePath = [NSString stringWithFormat:@"%@-%@-%@", entity2.name,entity2.pcode, entity2.code]; } NSMutableArray *sub4 = [BGChinaAddressEntity mj_objectArrayWithKeyValuesArray:sub2]; NSArray*sortedArray = [sub4 sortedArrayUsingComparator:^NSComparisonResult(BGChinaAddressEntity *obj1,BGChinaAddressEntity *obj2) { return [obj1.code compare:obj2.code options:NSNumericSearch];//正序 }]; entity2.sub = [[NSMutableArray alloc] initWithArray:sortedArray]; } } } if(!isCommonUnitEmptyArray(sub3)) { NSArray*sortedArray = [sub3 sortedArrayUsingComparator:^NSComparisonResult(BGChinaAddressEntity *obj1,BGChinaAddressEntity *obj2) { return [obj1.code compare:obj2.code options:NSNumericSearch];//正序 }]; entity1.sub = [[NSMutableArray alloc] initWithArray:sortedArray];// NSLog(@"entity1.sub:%lu", (unsigned long)entity1.sub.count); } // [self.province addSafeObject:entity1]; } } } [PPSingleObject sharedInstance].addressArr = [NSMutableArray arrayWithArray:self.model.addressArr]; [[PPSingleObject sharedInstance] stroreAddress];}

加载本地数据:

if(0 == [PPSingleObject sharedInstance].provinceCityDistrictVersion || (sProvinceCityDistrictVersion != [PPSingleObject sharedInstance].provinceCityDistrictVersion)) { [PPSingleObject clearDataModelWithFileName:sAddressData]; [[PPSingleObject sharedInstance].addressArr removeAllObjects]; } else { [[PPSingleObject sharedInstance] loadLocationListDataBSD]; }

调用后台省市区接口

if(isCommonUnitEmptyArray([PPSingleObject sharedInstance].addressArr)) { [self excuteRegion_listCommand]; }

-(void)excuteRegion_listCommand{ @weakify(self); [[self.viewModel.region_listCommand execute:self.model]subscribeNext:^(id result) { @strongify(self); } error:^(NSError *error) { @strongify(self); }];}

-(CBPAddressManagerAddEntity *)model{ if(!_model) { _model = [[CBPAddressManagerAddEntity alloc]init]; _model.icon = [UIImage imageNamed:@"right_arrow"]; } return _model;}-(CBPAddressManagerViewModel *)viewModel{ if(!_viewModel) { _viewModel = [[CBPAddressManagerViewModel alloc]init]; } return _viewModel;}

@property (nonatomic, strong) CBPAddressManagerViewModel *viewModel;@property (nonatomic, strong) CBPAddressManagerAddEntity *model;

​​《省市区三级联动数据本地序列化和反序列化》​​。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:在viewWillAppear函数中禁止左滑,viewWillDisappear开启左滑动产生进入下一个页面来回滑动造成无法点击点击按钮问题
下一篇:https的证书错误,错误码-1012问题及解决方案
相关文章

 发表评论

暂时没有评论,来抢沙发吧~