2017-05-07 49 views
0

我正在实现一个简单的聊天视图控制器。在从firebase获取添加的孩子时,它将UI冻结,直到它给最后一个孩子。奇怪的行为。 下面是代码snippet-从firebase数据库获取添加的孩子,它冻结了UI

-(void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    chatArray=[NSMutableArray new]; 
    ref=(FIRDatabaseReference *)[[FIRDatabase database] reference]; 
    refHandleForGettingChatInfo=[[ref child:@"Chat"] observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot) 
    { 
    NSLog(@"New chat"); 
    NSDictionary<NSString *, NSString *> *message = snapshot.value; 
    [chatArray addObject:message]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [tblChat reloadData]; 
    }); 
    }]; 
} 

#pragma mark - Table View delegates and DataSources 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return chatArray.count; 
} 

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    static NSString * CellIdentifier [email protected]"ChatTableViewCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:CellIdentifier]; 
    } 

    cell.selectionStyle=UITableViewCellSelectionStyleNone; 
    NSDictionary *message=[chatArray objectAtIndex:indexPath.row]; 

    cell.textLabel.text=[message valueForKey:@"userName"]; 
    cell.detailTextLabel.text=[message valueForKey:@"userMessage"]; 

    return cell; 
} 

的Xcode版本:8.3.2

部署目标:10.2

如果有人面临着同样的问题,请让我知道什么是最合适的解决方案这种情况。谢谢

回答

0

更新答案:

NSOperationQueue *dataQue = [[NSOperationQueue alloc]init]; 
      [dataQue addOperationWithBlock:^{ 
       [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
        chatArray=[NSMutableArray new]; 
ref=(FIRDatabaseReference *)[[FIRDatabase database] reference]; 
refHandleForGettingChatInfo=[[ref child:@"Chat"] observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot) 
{ 
NSLog(@"New chat"); 
NSDictionary<NSString *, NSString *> *message = snapshot.value; 
[chatArray addObject:message]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
    [tblChat reloadData]; 
}); 
}); 
}]; 
       }]; 
      }]; 
+0

我已经取代我的听众代码与您的代码,还是死了。 –

+0

我更新了电话号码,然后重新加载**。在dispatch_get_main_queue我认为它会解决你的问题 –

+0

好吧,让我检查NAVEEN –