Naim Berk Tumer
Naim Berk Tumer

Reputation: 129

Firebase Database listen only specific childrens

I'm trying to make a chat application using firebase database but I'm having some problems. enter image description here
This is my database. I want to add a listener to Messages but just for the values for User->UID->chats->Chat IDs. How can I do this?

Upvotes: 0

Views: 400

Answers (2)

koceeng
koceeng

Reputation: 2165

You need to do it twice: first get the chat id list based on user id, then get the messages data for each of those chat ids.

// consider you have you user (FirebaseUser) object ready
FirebaseDatabase.getInstance().getReference("Users/" + user.uid + "/chats")
    .addListenerForSingleValueEvent(new ValueEventListener() {
        ... onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                FirebaseDatabase.getInstance().getReference("Messages/"+snapshot.getKey())
                    .addListenerForSingleValueEvent(...
                        ... onDataChange(DataSnapshot messageSnapshot) {
                            // I changed the snapshot name to make it easier to read
                            // here messageSnapshot.getKey() is message ID
                            // and messageSnapshot.getValue() will contain data you need (I think)
                        }
                        ...
                    );
            }
        }
        ...
    });

Hope this helps

Upvotes: 1

Ege Kuzubasioglu
Ege Kuzubasioglu

Reputation: 6282

Here is an example to access

 final FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference reference = database.getReference("Messages").child(read);

Upvotes: 0

Related Questions