微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

同时调用的 Firestore 查询“onSnapshot”不起作用

如何解决同时调用的 Firestore 查询“onSnapshot”不起作用

我使用 Ionic 和 Firestore 创建了一个具有实时聊天功能的应用,但我遇到了问题。

对话加载方法

refUneConversationMyUserCol.ref.orderBy('date','desc').limit(20).get()

为此添加一个“onSnapshot”请求以检索实时发送的最后一条消息

 this.unsubscribedataUneConversation = refUneConversationMyUserCol.ref.orderBy('date','desc').limit(1).onSnapshot(result => {
    console.log(result.docs[0].data());
    if (this.isCalledBySnapshot === false) {
      this.isCalledBySnapshot = true;
    } else if (result.docs[0].data().expediteur !== this.authentificationService.uidUserActif) {
      const data = result.docs[0].data();
      const id = result.docs[0].id;
      this.dataUneConversation.push({ id,...data } as UneConversation);
    }
  });

它可以完美运行,但是当我同时发送一条消息时(两个不同的帐户相互交谈),我遇到了一个问题,onSnapshot 只触发一次,我只收到一条消息。

我指定两条消息在数据库中发送良好,只是在实时会话期间不会同时显示

你知道为什么吗?

谢谢

(这里是整个方法

  async getDataUneConversation(idI: string) {
if (this.loadedDataUneConversation !== idI) {
  /* ANCHOR Msg en direct */
  this.isCalledBySnapshot = false;
  if (this.unsubscribedataUneConversation) {
    await this.unsubscribedataUneConversation();
  }

  const refUneConversationMyUserCol = this.afs.collection<User>('users').doc<User>(this.authentificationService.uidUserActif).collection<Conversations>('conversations');
  const result = await refUneConversationMyUserCol.ref.orderBy('date','desc').limit(20).get();

  /* ANCHOR Msg en direct */
  this.unsubscribedataUneConversation = refUneConversationMyUserCol.ref.orderBy('date',...data } as UneConversation);
    }
  });

  /* ANCHOR Msg en brut */
  if (result.docs.length < 20) {
    this.infiniteLastUneConversationMax = true;
  } else {
    this.infiniteLastUneConversationMax = false;
  }
  this.infiniteLastUneConversation = result.docs[result.docs.length - 1];
  this.dataUneConversation = result.docs.map(doc => {
    const data = doc.data();
    const id = doc.id;
    return { id,...data } as UneConversation;
  });
  this.dataUneConversation.reverse();
  this.loadedDataUneConversation = idI;
}

}

编辑工作:

    this.unsubscribedataUneConversation = refUneConversationMyUserCol.ref.orderBy('date','asc').startAfter(this.dataUneConversation[this.dataUneConversation.length
- 1].date).onSnapshot(result => {
            result.docs.forEach(element => {
              const data = element.data();
              const id = element.id;
              if (!this.dataUneConversation.some(e => e.id === element.id)) {
                this.dataUneConversation.push({ id,...data } as UneConversation);
              }
            });
          });

解决方法

您将实时消息限制为最后一条消息。在聊天应用程序中,您想收听所有新消息。所以问题可能出在您的 .limit(1) 子句中。

但是,如果您这样做,我知道您将收到自对话开始以来的整个对话以及所有消息。

我的方法是这样的:

  1. 从您的 refUneConversationMyUserCol... 对话加载器中获取最后一条消息的日期。
  2. 当您执行 onSnapshot() 获取最后一条消息时,不要限制为 1 条 消息,而是从上次加载消息的日期之后的某个日期开始.

因为无论如何您都是按日期订购的,所以这将很容易解决。查看“Adding a cursor to your query”。

基本上,您会对 Firestore 说:给我实时新消息,但从现在开始 - 即使同时发布了许多消息,您也会收到所有消息,因为您不仅限于1.

如果这还不够清楚,请随时提问。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。