2014-09-23 74 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