0

모든 사용자를 불러 오기 위해 블록을 실행 중입니다. Quickblox 계정으로 연결합니다. 그러나 사용자의 로그인 이름을 변경 가능한 배열에 추가 한 후에도 배열의 수는 여전히 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]; 
     } 

    } errorBlock:^(QBResponse *response) { 

    }]; 
+1

여기서이 코드와 관련하여 배열을 사용하고 있습니까? 'usersForPage' 함수가 비동기식 인 경우 배열의 크기를 확인하고 있습니다. 나중에 배열에 액세스하려고하면 메서드의 완료 핸들러가 아직 실행되지 않았으므로 빈 상태가됩니다. – Fonix

+0

사용하고 있습니다. viewDidLoad에서이 코드는 '_contacts'가 배열인데, 저는 tableVIewCell의 labelText에서 배열을 사용하고 싶습니다 ... 저는 여러분이 함수가 비동기 적이라고 생각합니다. 어떻게해야합니까? –

+1

흠, 그냥이 스 니펫에서 말하기 힘들지 만, 당신의 테이블이'_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

시도했지만 .. 문제는 또한 설정해야합니다. 행 개수는 배열 개수에 따라 다르지만 블록 수만큼 행 메서드가 호출됩니다. –

+0

흠.하지만 그 코드는'[self.tableview reloadData];에서 시작해야합니다.이 시점에서'_contacts' 배열이 채워질 것입니다. 다른 곳에서 데이터를 다시로드하지 않습니까, 아니면 일찍 일어날 수있는 다른 tableview 메소드를 사용하고 있습니까? – Fonix

+0

numberOfRows는 관계없이 테이블 뷰 초기 생성시 호출 될 수 있으므로 조기에 실행되지만 완료 블록의'[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; 
관련 문제