我是AfNetworking的新手,我想在運行我的函數後得到數組,但是我總是在崩潰,因爲它很晚纔將數據加載到數組中,有什麼方法可以阻止它直到它將所有數據加載到數組中?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];
}
什麼是你得到的崩潰?您不應該讓任何線程等待,您應該在進程完成時觸發更新(這是您的代碼已經寫入的方式)。 – Wain 2014-09-23 06:23:14
我得到超出限制的碰撞指數 – NullData 2014-09-23 06:23:56
哪裏?顯示異常消息和堆棧跟蹤,並突出顯示它發生的行。 – Wain 2014-09-23 06:24:35