Reputation: 1925
I'm creating an review's app using Firebase Database,
Each user, has Profile (displayName, image, age and gender), and I allow the users to change their profile name and image, but, when user change it name all the post reviews keeps the old name (offcurse, I didn't change them), so, I created a method that run everytime user change it name -
mFirebaseDatabaseReference.child("displayName").setValue(newName);
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference dataRef = database.getReference();
dataRef.child("reviews").orderByChild("uid").equalTo(mFirebaseUser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot snapshot) {
for (DataSnapshot data : snapshot.getChildren()) {
data.child("userName").getRef().setValue(newName);
}
}
});
It's Work like a charm, but, my problem is the reviews-comments,
How can I sort them to be equal to my UID?
I guess that I need something like "deep ordering"
project structure -
Upvotes: 0
Views: 1004
Reputation: 598728
Firebase Database queries can only sort on a property one level deep or in a fixed path. So you'll need to augment your data model to allow this use-case.
The simplest way is to keep a mapping from the UID to their comments, which is probably also useful to show a user all their comments. It would look like this:
"userComments": {
"uidOfNirel": {
"-KfpD...._-Kfpu...": "-KfpD..../-Kfpu..."
}
}
So the value is the actual path to a specific comment, allowing you to quickly reconstruct the full reference to each comment. The keys are just encoded versions of the path to ensure uniqueness without generating yet another push ID. You'd then get all comments of the user and loop over them with:
dataRef.child("userComments").child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot commentSnapshot: snapshot.getChildren()) {
System.out.println(commentSnapshot.getKey());
DatabaseReference commentRef = dataRef.child("reviews-comments").child(commentSnapshot.getValue(String.class));
commentRef.addListenerForSingleValueEvent(...
}
});
Upvotes: 0