ios - Afnetworking late response -
i new afnetworking, want array after running function crash loads data array late, there way stop till loads data array?
-(void) getmodeldetails :(nsstring*)brandname completionhandler:(void (^)(id array))success { nsstring *brand = [brandname stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; [mbprogresshud showhudaddedto:self.view animated:yes]; afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager]; nsstring *link = [nsstring stringwithformat:@"http://phablet-fix.com/mob/get-model-details.php?model=%@",brand]; nslog(@"%@",link); manager.responseserializer.acceptablecontenttypes = [manager.responseserializer.acceptablecontenttypes setbyaddingobject:@"text/html"]; [manager get:link parameters:nil success:^(afhttprequestoperation *operation, id responseobject) { nslog(@"json: %@", responseobject); nsmutablearray *dataarray = [[nsmutablearray alloc] init]; nsdictionary *returneddealdict = responseobject ; nsarray *returnarray = [returneddealdict objectforkey:@"modeldetails"]; for(nsdictionary *dealdict in returnarray) { model = [[modeldc alloc] init]; model.modelid = [[dealdict objectforkey:@"id"] intvalue]; model.model = [dealdict objectforkey:@"model"]; model.mdevicebrand = [dealdict objectforkey:@"device_brand"]; model.mdevicetype = [dealdict objectforkey:@"device_type"]; model.mprotectionplanavilable = [dealdict objectforkey:@"protection_plan_available"]; model.msilverplan1year = [dealdict objectforkey:@"silver_plan_price_1year"]; model.msilverplan2year = [dealdict objectforkey:@"silver_plan_price_2year"]; model.msilverplan3year = [dealdict objectforkey:@"silver_plan_price_3year"]; model.mgoldplan1year = [dealdict objectforkey:@"gold_plan_price_1year"]; model.mgoldplan2year = [dealdict objectforkey:@"gold_plan_price_2year"]; model.mgoldplan3year = [dealdict objectforkey:@"gold_plan_price_3year"]; [dataarray addobject:model]; } success(dataarray); [mbprogresshud hidehudforview:self.view animated:yes]; if (dataarray.count == 0) { alert_view(@"please check internet connection."); [mbprogresshud hidehudforview:self.view animated:yes]; } else { } } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"error: %@", error); alert_view(@"error occured while loading data."); [mbprogresshud hidehudforview:self.view animated:yes]; }]; }
and in tableview 0 data array
- (void)tableview:(uitableview *)tableview didselectrowatindexpath :(nsindexpath *)indexpath { model = [planarray objectatindex:indexpath.row]; lblselectyourdevice.text = model.selectedmodel; tblview.hidden = yes; modeldetailarray = [[nsmutablearray alloc] init]; [self getmodeldetails:model.selectedmodel completionhandler:^(id array) { modeldetailarray = array; }]; nslog(@"%d",modeldetailarray.count); model = [[modeldc alloc] init]; model = [modeldetailarray objectatindex:indexpath.row]; }
this never work:
modeldetailarray = [[nsmutablearray alloc] init]; [self getmodeldetails:model.selectedmodel completionhandler:^(id array) { modeldetailarray = array; }]; nslog(@"%d",modeldetailarray.count); model = [[modeldc alloc] init]; model = [modeldetailarray objectatindex:indexpath.row];
because creating empty array, asking populated data , using without waiting population complete (or checking data want obtained).
change to:
modeldetailarray = nil; [self getmodeldetails:model.selectedmodel completionhandler:^(id array) { modeldetailarray = array; nslog(@"%d", modeldetailarray.count); if (modeldetailarray.count > indexpath.row) { model = [modeldetailarray objectatindex:indexpath.row]; // trigger ui update or next piece of processing here } else { // deal error } }];
note isn't creating empty objects aren't going use.
Comments
Post a Comment