使用Firebase-UI(Android)的Firestore聊天应用 [英] Firestore chat app using Firebase-UI (Android)

查看:613
本文介绍了使用Firebase-UI(Android)的Firestore聊天应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用适用于Cloud Firestore的Firebase-UI库创建聊天。



我想得到如下消息:

 消息1 
消息2
消息3
消息4
...

使用默认顺序,我得到如上所示的消息,但 limit 指令将快照从1切换到50,并且消息51 (最新的)不是r etrieved。

解决方案

要解决此问题,您可以使用以下查询:

 查询sChatQuery = sChatCollection 
.orderBy(timestamp,Query.Direction.ASCENDING)
.whereGreaterThanOrEqualTo(timestamp,desiredTime)
.limit(50);

如果您使用 RecyclerView 来显示数据,最简单的方法是使用以下代码:

  LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);

此approch会根据您的要求撤销订单。



另一种方法是创建自己的适配器,扩展 FirebaseListAdapter 并覆盖 getItem()方法,如这个:

  @Override 
public HashMap getItem(int pos){
return super.getItem(getCount) () - 1 - pos);
}

另一种方法是从数据库中获取数据,将其添加到数据库中 Collection List 将是一个很好的解决方案,然后使用 Collections.reverse(yourList) ;


I am trying to create a chat using the Firebase-UI library for Cloud Firestore. This github repository contains the relevant code which I am using. The problem comes with the order of the query.

See that the query is specified as:

Query sChatQuery = sChatCollection.orderBy("timestamp").limit(50);

However, I am getting the oldest 50 messages, instead of the newest ones, in the correct order (from old to new). On the other hand, I can use the following query:

Query sChatQuery = sChatCollection.orderBy("timestamp", Query.Direction.DESCENDING).limit(50);

and this retrieves the 50 newest messages, but in the wrong order (newest on top, oldest on bottom). Hence I do not know how to get this right.

I could locally reverse again the result from the query, but I cannot figure out how to do that (I have already gone through the FirebaseUI library with no luck).

EDIT

I have my chat subcollection for each event in my events collection:

events/event_doc/chat/chat_doc

And I would like to get the messages as follows:

Message 1
Message 2
Message 3
Message 4
...

With the default order I get the messages like shown above, but the limit instructions cuts the snapshots from 1 to 50, and the Message 51 (the newest) is not being retrieved.

解决方案

To solve this, you can use the following query:

Query sChatQuery = sChatCollection
    .orderBy("timestamp", Query.Direction.ASCENDING)
    .whereGreaterThanOrEqualTo("timestamp', desiredTime)
    .limit(50);

If you are using a RecyclerView to display data, the simplest way would be to use the following code:

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);

This approch will reverse you the order as you want.

Another approach would be to create your own adapter that extends FirebaseListAdapter and override getItem() method like this:

@Override
public HashMap getItem(int pos) {
    return super.getItem(getCount() - 1 - pos);
}

Another approach would be to get the data from the database, add it to a Collection, a List would be a good solution and then use Collections.reverse(yourList);.

这篇关于使用Firebase-UI(Android)的Firestore聊天应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆