2014-09-23 55 views
0

我是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]; 

} 
+0

什么是你得到的崩溃?您不应该让任何线程等待,您应该在进程完成时触发更新(这是您的代码已经写入的方式)。 – Wain 2014-09-23 06:23:14

+0

我得到超出限制的碰撞指数 – NullData 2014-09-23 06:23:56

+0

哪里?显示异常消息和堆栈跟踪,并突出显示它发生的行。 – Wain 2014-09-23 06:24:35

回答

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 
    } 
}]; 

注意这一点,你不打算使用也不会创建空的对象。

+0

我的tableview不是空的..我必须做的是获取参数对于下一个Web服务何时选择行被称为..之后,它崩溃 – NullData 2014-09-23 06:41:30

+0

我怎么能延迟..等待调用完整的web服务? – NullData 2014-09-23 06:43:01

+0

你不应该那样做。如果需要显示活动指示符,但不要让线程等待。 – Wain 2014-09-23 06:47:09