简体   繁体   English

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

[英]Ionic + Firebase - Real time database not updating view

I have a really weird problem with my application. 我的申请有一个很奇怪的问题。 In order for the real time database to display, I have to interact with the application. 为了显示实时数据库,我必须与应用程序进行交互。 2 months ago I did not have this problem, the information appeared normally, but now, I have to click on a button or something in order for the view to update. 2个月前我没有出现此问题,信息正常显示,但是现在,我必须单击按钮或其他内容才能更新视图。

I made a quick GIF here: 我在这里做了一个快速的GIF:

If you pause just as I click the <- back button you'll see what I mean.. https://i.gyazo.com/44b450aa502dc7f37e5a5feea19824c6.mp4 如果您在我单击<- back按钮时暂停,您将明白我的意思.. https://i.gyazo.com/44b450aa502dc7f37e5a5feea19824c6.mp4

I don't know what I did to make this happen.. 我不知道该如何做。

Here is a code example from my application: 这是我的应用程序中的代码示例:

.ts : .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 : .html

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

So this code above used to work perfectly, but now I have to interact with my app for it to display on the screen, even though the postFeed console logs perfectly fine. 因此,上面的这段代码曾经可以完美地运行,但是现在我必须与我的应用进行交互,以使其在屏幕上显示,即使postFeed控制台记录得很好。 The data shows up in the console exactly when I request it, but it doesn't show on screen. 数据完全在我请求时显示在控制台中,但没有显示在屏幕上。

Any ideas? 有任何想法吗? As I said, it used to work fine. 就像我说的那样,它过去工作正常。 Any help would be great! 任何帮助将是巨大的! Thank you 谢谢

For anyone else who has this problem, NgZone was the answer for me. 对于其他有此问题的人, NgZone是我的答案。

I imported it: 我导入了它:

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

added it to the constructor: 将其添加到构造函数中:

public zone: NgZone,

and then wrapped it around my firebase code: 然后将其包装在我的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())
     }):
  });

and this worked wonderfully for me. 这对我来说很棒。 I couldn't find the actual cause of the issue, but I just wrapped the NgZone code around all my firebase functions and it worked like it used to. 我找不到导致此问题的真正原因 ,但我只是将NgZone代码包装在我的所有NgZone函数周围,并且工作起来像以前一样。 If anyone else has more information that would be great. 如果其他人有更多的信息,那将是很好的。

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

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