简体   繁体   English

如何将打开的房间的聊天记录存储在Firebase中的唯一位置,然后进行检索?

[英]How can I store the opened room's chat at unique locations in Firebase and then retrieve them?

I want to do something like this: suppose there are 5 chat rooms and the user can enter and chat in any chat room by pressing the respective button. 我想做这样的事情:假设有5个聊天室,并且用户可以通过按相应的按钮进入并在任何聊天室中聊天。 So, he should be shown the chats of that room only and not overall chat. 因此,应该只向他显示该房间的聊天,而不是整个聊天。 At what reference in the Firebase Database do these chats should be stored so that every time when user enters a room, he is shown only that room's chat? 这些聊天记录应存储在Firebase数据库中的什么引用上,以便每次用户进入房间时,仅向他显示该房间的聊天记录?

One way I thought is by generating timestamp and storing the chat under it in Firebase Database and then fetching the chats from there, but when I'm trying to this a new timestamp is getting generated every time and the previous one under which chats are stored in the database is getting lost. 我认为的一种方法是生成时间戳并将其下的聊天记录存储在Firebase数据库中,然后从那里获取聊天记录,但是当我尝试这样做时,每次都会生成一个新的时间戳记,而上一个存储聊天记录的时间戳记在数据库中迷路了。

Here's how I'm generating the timestamp: 这是我生成时间戳的方法:

Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();

Here's how I'm storing chat under it: 这是我在其下存储聊天的方式:

mFirebaseDatabaseReference.child("chatmodel").child(ts).setValue(model);

How do I achieve this? 我该如何实现? Please help me. 请帮我。

To achieve this I would suggest you to re-structure your database. 为此,我建议您重新构建数据库。 Avoid too much nesting and post the chat rooms/chat requests like this: 避免过多的嵌套,并像这样发布聊天室/聊天请求:

So here you see two chat rooms under unique ids generated by .push() at the time of posting request. 因此,在发布请求时,您会在.push()生成的唯一ID下看到两个聊天室。 With one unique identifier for request with key uniqueCharacter . 通过密钥uniqueCharacter使用一个唯一的请求标识符。

{
      "-HFSSFG5647DDHD" : {
        "name" : "Some Name",
        "someOtherinfo" : "test",
        "timestamp" : 12443567,
        "uniqueCharacter" : "Request2"
      },
      "-YYTH6637FRFSHt5" : {
        "name" : "Some other name",
        "someOtherinfo" : "test",
        "timestamp" : 12443567,
        "uniqueCharacter" : "Request3"
      }
    }

Now the other user sees both these requests and once he clicks on some you get the unique id generated at the time of pushing and create a separate chat room for that particular push id using filters like this: 现在,另一个用户看到了这两个请求,并且在单击某些用户时, 您将获得推送时生成的唯一ID,并使用如下过滤器为该特定推送ID创建单独的聊天室:

If user selected request3 then your database ref will be: 如果用户选择了request3,那么您的数据库引用将是:

databaseRef.orderByChild("uniqueCharacter").equalTo("Request3");

This will give you access to that particular push id which you can share among all those who chose this and create a common chat room with same id for all of them. 这将使您能够访问特定的推送ID,您可以在所有选择此推送ID的用户之间共享该ID,并为所有人创建一个具有相同ID的公共聊天室。

Hope you get the basic idea here on achieving what you want. 希望您在这里获得实现所需目标的基本思路。

This is not a production example but just a basic one to give you an idea about better way of achieving your goal. 这不是生产示例,而只是一个基本示例,可让您了解实现目标的更好方法。

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

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