CONTEXT
I am currently working on a Firebase application (in Android). I have set up authentication, storage and the database with no issues to speak of. As the Firebase
I used two approaches to solve this. One is to have that extra field, thus having the redundancy, and the another is to use a HashMap to map the User's POJO with it's UID.
If you have noticed that whenever you load the data from the Firebase Database, mostly it's in the form of a Map, so all I did was to save the key to a Map, which uses that key to map the User's POJO. That map was stored in a singleton so it could be accessed from anywhere in the app.
Something like this -
DatabaseReference mFirebaseRef = FirebaseDatabase.getInstance().getReference("users"); //Add your filter as your requirements. I am currently loading all the users.
mFirebaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map map = (Map) dataSnapshot.getValue();
if (map != null) {
for (String uid : map.keySet()) {//keys will be uid
Map userMap = map.get(uid); //userMap is a single user's data.
User user = new User(userMap); //I have used a constructor to map the populate the properties of the User Model.
getSingletonMap().push(uid,user); //pushing using the uid
}
}
}
This way I can get the user just by using the UID ( getSingletonMap().get(uid)
)or can get the uid of a user. I hope this answers your question.