0

I made private chat for users and wanna read the list of all users i chatted with them so i add users in list but it duplicate users :-

FirebaseDatabase myMessage = FirebaseDatabase.getInstance(); 
final DatabaseReference mine = myMessage.getReference(facebookUserName +`` "myMessageList").push();
mine.setValue(new MyMessageListModel(to));
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Abdelaziz refaat
  • 157
  • 1
  • 12
  • There is no unique index in firebase. You should first query to check if user exists in the list, otherwise add a new one. To be consistent, you should also do it inside a transaction. – abeyaz Feb 22 '18 at 13:00
  • i checked first is facebookUserName + "myMessageList" is exist or no , and checked if the user inside it or no , but i doesn't work and i don't know too much about Transaction – Abdelaziz refaat Feb 22 '18 at 13:02
  • 1
    https://firebase.google.com/docs/firestore/manage-data/transactions – abeyaz Feb 22 '18 at 13:12
  • 1
    the only way to get an unique user is getting user UID with mAuth.getCurrentUser().getUid(); , and then you can just push random keys of chats inside e ach user id , and connect those ids with the ones who chat, so the structure would be like this , users - userUID - peopleWhoChatUID - push messages inside – Gastón Saillén Feb 22 '18 at 13:38
  • 1
    If you want to make something unique in the Firebase Database, you should typically not store it by calling `push()` (which generates a unique ID when you call it) but by calling `child(thingThatIsUnique)`. Using the unique values as keys ensures they'll always be unique, since keys are already unique in the data model. For more on ensuring uniqueness, see https://stackoverflow.com/q/35243492 (and the questions I linked from there). – Frank van Puffelen Feb 22 '18 at 14:28

0 Answers0