繁体   English   中英

从firebase数据库获取添加的子项,它冻结UI

[英]Getting added child from firebase database, It freezes UI

我正在实现一个简单的聊天视图控制器。 从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 =@"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

如果有人遇到过相同的问题,请告诉我哪种是最适合此方案的解决方案。 谢谢

更新的答案:

 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];
 });
 });
}];
                }];
            }];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM