无法在块内添加对象

时间:2016-10-21 08:13:18

标签: ios objective-c nsmutablearray objective-c-blocks quickblox

我正在运行一个块来获取所有用户与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) {

    }];

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;