0

I'm making now a chat app by using the Firestore database and im thinking how to avoid the same chat to be created twice.

For example, if person A sends message to person B, I had like the message to enter the same chat collection as if person B send the message to person A.

I have found the following suggestion here

It recommends to compare the users UID which I know and construct some chatID based on the combination of those string.

private String setOneToOneChat(String uid1, String uid2)
{
    if(uid1 <uid2){
        return uid1+uid2;  
    }
    else{
        return uid2+uid1;
    }
}

comparing the length of both UID doesn't work since all (or at least all I have seen are from the same length).

However, first I didn't really understand how to use math operator such as < on a string, second im not sure if it really catches all cases.

If there are any suggestions to implement such thing I would like to hear.

Thank you

Ben
  • 1,737
  • 2
  • 30
  • 61
  • 1
    You'll typically want to model chat rooms as entities in your database, for example by having a separate collection for each chat room. In that case, one way is to simply concatenate some ID for each user in a lexicographical order to determine the ID of the room, as I showed in my answer here: https://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase. Note that the comparison should be about the actual order of the content, not just comparing their length. – Frank van Puffelen Oct 10 '19 at 22:39
  • If you consider at some point in time to try using Kotlin, here you can find a tutorial on how to create a complete and functional [Firestore Chat App](https://www.youtube.com/playlist?list=PLn2n4GESV0Ak1HiH0tTPTJXsOEy-5v9qb). – Alex Mamo Oct 11 '19 at 11:19

1 Answers1

0

The ordering should be based on the actual content of the strings, not just on their length.

So in Java/Android:

if(uid1.compareTo(uid2) > 0){
    return uid1+uid2;  
}
else{
    return uid2+uid1;
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807