Reputation: 2067
I am new to AfNetworking, i want to get array after running my function but i always get crash as it loads data into array very late, is there any way to stop it till it loads all data into 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 your 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 my tableview i get zero data into my 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];
}
Upvotes: 0
Views: 183
Reputation: 119031
This will 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 you are creating an empty array, asking for it to be populated with data and then immediately using it without waiting for the population to complete (or checking that the data you want was actually 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 the UI update or next piece of processing here
} else {
// deal with the error
}
}];
Note that this also isn't creating empty objects that you aren't going to use.
Upvotes: 1