0
votes

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

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: stackoverflow.com/questions/33540479/…. Note that the comparison should be about the actual order of the content, not just comparing their length. - Frank van Puffelen
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. - Alex Mamo

1 Answers

0
votes

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;
}