【问题标题】:Ionic + Firebase - Real time database not updating viewIonic + Firebase - 实时数据库不更新视图
【发布时间】:2018-11-19 00:52:15
【问题描述】:

我的应用程序有一个非常奇怪的问题。为了显示实时数据库,我必须与应用程序交互。 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 控制台日志非常好。 数据会在我请求时准确地显示在控制台中,但不会显示在屏幕上。

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

【问题讨论】:

  • 我的代码 sn-p 最近也坏了,我没有更新任何代码或本地依赖项。因此,似乎值得检查 firebase 最近是否进行了任何重大更改..

标签: javascript angular ionic-framework firebase-realtime-database ionic2


【解决方案1】:

对于遇到此问题的其他人,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 函数中,它就像以前一样工作。如果其他人有更多信息,那就太好了。

【讨论】:

  • 在你开始使用 WKWebView 之后就开始发生了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
  • 2021-10-04
  • 2021-10-19
  • 1970-01-01
相关资源
最近更新 更多