我是AfNetworking的新手,我希望在运行我的函数后得到数组,但我总是会崩溃,因为它很晚才将数据加载到数组中,有没有办法阻止它直到它将所有数据加载到数组中?
-(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];
}];
}
在我的tableview中,我的数据为零
- (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];
}
答案 0 :(得分:1)
这永远不会奏效:
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];
因为您正在创建一个空数组,要求使用数据填充它,然后立即使用它而不等待填充(或检查实际获得的数据)。
更改为:
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
}
}];
请注意,这也不会创建您不会使用的空对象。