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;