首页 > 解决方案 > Ionic / Firebase 应用程序在后台后建立连接缓慢

问题描述

我有一个使用 Firebase 和 Angular 构建的 Ionic 应用程序。

我遇到了一个问题,当用户在空闲后返回应用程序并向下滑动以刷新提要时,有时需要 30 秒才能加载。

返回应用程序后,大约 70% 的刷新时间是即时的,有时我会在 8 小时后返回并向下滑动以刷新,这实际上是即时的。

但是,30% 的时间需要长时间。也许30-40秒。它太长了,而且没有明显的原因。

我想知道是否有人知道这里可能存在什么问题。如果我不得不猜测我会说这是因为与 Firebase 的连接需要很长时间,但我不确定如何解决这个问题?它似乎也不是基于连接。

我的向下滑动代码:

  doRefresh(refresher) {
    this.goToNew(true);
    refresher.complete();
  }

并且this.goToNew()是:

  this.database.database.ref('/posts/'+this.feedId)
  .orderByChild('created')
  .limitToLast(10)
  .once('value', (snapshot) => {
    snapshot.forEach(child => {
        this.feedCounter.push(child.val());
        this.postFeed.push(child.val());
        this.postFeed.sort(function(a,b){
          return +new Date(b.created) - +new Date(a.created);
        });
        if(this.feedCounter.length >= 10) {
          this.firstRun = false;
        }
        return false;
    });

我不知道是什么导致了这个问题......有什么想法吗?

谢谢!

标签: firebasecordovaionic-frameworkfirebase-realtime-databaseionic3

解决方案


发现它实际上是一个 Firebase 身份验证错误。

从后台返回后无法检查用户的身份验证,因此我的修复onAuthStateChanged仅在平台为ready. 即时建立火力基地连接!

this.platform.ready().then(() => {
    this.afAuth.auth.onAuthStateChanged((user) => {
       //code here
 });
});

希望对某人有所帮助,因为这是一个奇怪的问题。


推荐阅读