Ionic + Firebase - 实时数据库不更新视图

Ionic + Firebase - Real time database not updating view

我的应用程序有一个非常奇怪的问题。为了显示实时数据库,我必须与应用程序交互。 2 个月前我没有这个问题,信息显示正常,但现在,我必须单击按钮或其他东西才能更新视图。

我在这里做了一个快速的 GIF:

如果你在我点击 <- back 按钮时暂停一下,你就会明白我的意思了.. https://i.gyazo.com/44b450aa502dc7f37e5a5feea19824c6.mp4

我不知道我做了什么让这一切发生..

这是我的应用程序中的代码示例:

.ts:

  if(fromClick) { this.subscription.off(); }
  this.postFeed = new Array();
  this.subscription = this.database.database.ref('/posts/'+this.locationId)
  .orderByChild('score')
  .limitToLast(10);
  this.subscription.on('child_added', (snapshot) => {
        this.postFeed.push(snapshot.val())
  });

.html:

  <ng-container *ngFor="let post of postFeed.reverse(); let i = index">
    <post [postData]="post"></post>
  </ng-container>

所以上面的这段代码过去工作得很好,但现在我必须与我的应用程序交互才能显示在屏幕上,即使 postFeed 控制台日志记录得很好。 当我请求时,数据会准确地显示在控制台中,但不会显示在屏幕上。

有什么想法吗?正如我所说,它曾经工作得很好。任何帮助都会很棒!谢谢

对于遇到此问题的任何其他人,NgZone 是我的答案。

我导入了它:

import { NgZone } from '@angular/core';

将其添加到构造函数中:

public zone: NgZone,

然后将其包裹在我的 firebase 代码中:

  this.zone = new NgZone({});
  if(fromClick) { this.subscription.off(); }
  this.postFeed = new Array();
  this.subscription = this.database.database.ref('/posts/'+this.locationId)
  .orderByChild('score')
  .limitToLast(10);
  this.subscription.on('child_added', (snapshot) => {
    this.zone.run(() => {
        this.postFeed.push(snapshot.val())
     }):
  });

这对我来说效果很好。我找不到问题的实际 原因,但我只是将 NgZone 代码包裹在我所有的 firebase 函数周围,它像以前一样工作。如果其他人有更多信息,那就太好了。