0

我正在運行一個塊來獲取所有用戶與Quickblox帳戶的連接。但是在將用戶的登錄名添加到可變數組後,數組的計數仍然顯示爲零或爲空數組。無法在塊內添加對象

QBGeneralResponsePage *page = [QBGeneralResponsePage responsePageWithCurrentPage:1 perPage:10]; 
    [QBRequest usersForPage:page successBlock:^(QBResponse *response, QBGeneralResponsePage *pageInformation, NSArray *users) { 

     for (int i =0; i<users.count; i++) { 
      QBUUser *user = [[QBUUser alloc]init]; 
      [_contacts insertObject:user.login atIndex:i]; 
     } 

    } errorBlock:^(QBResponse *response) { 

    }]; 
+1

你在哪裏使用與該代碼相關的數組?也就是你在哪裏檢查數組的大小,如果'usersForPage'函數是異步的,如果你試圖直接訪問數組,它將是空的,因爲該方法的完成處理程序還沒有執行 – Fonix

+0

我正在使用這個代碼在viewDidLoad,其中'_contacts'是數組,我想使用tableVIewCell的labelText中的數組....我認爲你是正確的功能是異步的..我該怎麼辦? –

+1

hmm,很難從這段代碼中知道,但我假設你的表正在將它的單元格放在'_contacts'數組中,也許你只需要從完成處理程序重新加載tableview,它就會工作。否則你可能需要做任何你需要做的內部完成處理程序,而不是在函數調用 – Fonix

回答

0

我有一種感覺,你要使用的陣列之前完成處理已實際執行

QBGeneralResponsePage *page = [QBGeneralResponsePage responsePageWithCurrentPage:1 perPage:10]; 
[QBRequest usersForPage:page successBlock:^(QBResponse *response, QBGeneralResponsePage *pageInformation, NSArray *users) { 

    for (int i =0; i<users.count; i++) { 
     QBUUser *user = [[QBUUser alloc]init]; 
     [_contacts insertObject:user.login atIndex:i]; 
    } 

    [self.tableview reloadData]; //possible fix 

    //move any other code that uses _contacts here, or call a function that will deal with _contacts 

} errorBlock:^(QBResponse *response) { 

}]; 

//if you are trying to use _contacts here, its size will be 0 because the above completion handler has not executed yet. 
//consider moving whatever is here into the completion handler above 
+0

我試過了..但問題是我也必須設置行的數量根據數組數量..但numberOf行方法在塊執行前被調用。 –

+0

嗯,但這隻應該從'[self.tableview reloadData];''這將觸發'_contacts'數組將被填充,如果你有像我所示的代碼。你確定你沒有在其他地方重新加載數據,或者使用其他可能導致過早發生的tableview方法? – Fonix

+0

numberOfRows可能會在tableviews初始創建時調用,因此可能會過早引發,但是一旦發生完成塊中的[self.tableview reloadData];就會再次觸發,然後您的表應報告正確的行數從該函數返回_contacts的大小 – Fonix

0

這是所有關於邏輯,我想它了,因爲它點擊在我的腦海簡單一線解決方案。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"]; 
      //cellLabelText will not get filled by array until the block api gets data in array 

      if (_contacts.count>0) { 
        cell.textLabel.text = [arrContacts objectAtIndex:indexPath.row]; 
      } 

      return cell;