In Firebase, when applying the principles of flattened data structure explained here.
If I have the structure used in their example:
// Chats contains only meta info about each conversation
// stored under the chats's unique ID
"chats": {
"one": {
"title": "Historical Tech Pioneers",
"lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
"timestamp": 1459361875666
},
"two": { ... },
"three": { ... }
},
// Conversation members are easily accessible
// and stored by chat conversation ID
"members": {
// we'll talk about indices like this below
"one": {
"ghopper": true,
"alovelace": true,
"eclarke": true
},
"two": { ... },
"three": { ... }
},
"users": {
"ghopper": {
"name": "user_name",
"age": 28
},
"alovelace": { ... },
"eclarke": { ... }
}
}
let's say that I want to show the list of members of channel one
with all their details. That means that I have to firstly retrieve all children at the location members/one
, and then query each of the users ID individually.
That means that if I want to show them in a recycler view, the retrieving of data being asynchronous, user experience may not be the best:
Which would in my case be an implementation like:
@Override
public void onBindViewHolder(final UserViewHolder viewHolder, int position) {
//getKey(position) just retrieve the key of current member in my adapter array
FirebaseDatabase.getInstance().getReference("users").child(getKey(position)).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
viewHolder.setName(user.getName());
viewHolder.setPersonAge(user.getAge());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
This is not ideal, but I cannot see another approach. Loading each member before launching the activity introduce the problem of knowing when all the users profiles have been retrieved from the database.
Any advice is welcomed.