【问题标题】:Getting added child from firebase database, It freezes UI从firebase数据库中获取添加的孩子,它冻结了用户界面
【发布时间】:2017-05-07 17:29:11
【问题描述】:

我正在实现一个简单的聊天视图控制器。在从 Firebase 添加孩子时,它会冻结 UI,直到它给出最后一个孩子。奇怪的行为。 这是代码sn-p-

-(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

如果有人遇到过同样的问题,请告诉我最适合这种情况的解决方案。谢谢

【问题讨论】:

    标签: ios objective-c uitableview firebase firebase-realtime-database


    【解决方案1】:

    更新答案:

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

    【讨论】:

    • 我已经用你的代码替换了我的监听器代码,它仍然挂起。
    • 我更新了调用你的** reloadata**。在 dispatch_get_main_queue 我认为它会解决你的问题
    • 好的,让我检查一下 NAVEEN
    • Naveen 没有任何改进,dispatch_get_main_queue() 是个好主意。在主线程中重新加载 tableview,但 firebase 块太快而无法在主线程上处理。我在 firebase 数据库中有 15 条记录,它会挂起 UI,直到它给我第 15 条记录。
    猜你喜欢
    • 2017-04-07
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多