Help with recovering Firebase nodes

Asked

Viewed 133 times

0

I want to recover all users' followers in firebase.

I have the following structure at Firebase:

-seguidores
    -id amigo
        -id usuario 

For that structure I did the following to retrieve it:

    public void onDataChange(DataSnapshot dataSnapshot) {
        //Recupera dados de usuário logado
        usuarioLogado = dataSnapshot.getValue( Usuario.class );
        /*
         * Recuperar seguidores */
        DatabaseReference seguidoresRef = firebaseRef
                .child("seguidores")
                .child( idUsuarioLogado );
        seguidoresRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                seguidoresSnapshot = dataSnapshot;
                dialog.cancel();
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

And I went like this:

for( DataSnapshot seguidores: seguidoresSnapshot.getChildren() ){}

In this way I recover all through user id.

Now if my structure were like this:

-usuarios
    -id usuario 

Using this snippet of code:

DatabaseReference usuariosRef = firebaseRef
        .child("usuarios");
usuariosRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        usuariosSnapshot = dataSnapshot;

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

How I would make a for to recover the ids of this type of structure?

1 answer

0


If I understand your structure correctly, let’s take an example:

- usuarios
    - idDouSUAriOAWcjvS_jG
        - id: "idDouSUAriOAWcjvS_jG"
        - nome: "Francisco João"
        - idade: 18

The user class may look like this:

public class Usuario {
    private String id;
    private String nome;
    private int idade;

    public Usuario() {}

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade = idade;
    }

}

To recover only (but all) ids:

DatabaseReference idsRef = firebaseRef.child("usuarios");
idsRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> ids = new ArrayList();

        for (DataSnapshot child : dataSnapshot.getChildren()){
            Usuario u = child.getValue(Usuario.class);
            ids.add(u.getId());
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) { }
});

To recover all users:

DatabaseReference usuariosRef = firebaseRef.child("usuarios");
usuariosRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<Usuario> usuarios = new ArrayList();

        for (DataSnapshot child : dataSnapshot.getChildren()){
            Usuario u = child.getValue(Usuario.class);
            if (u != null) {
                usuarios.add(u);
            } 
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) { }
});

To recover a user by Id:

DatabaseReference usuarioRef = firebaseRef.child("usuarios").orderByChild("id").equalTo("idDouSUAriOAWcjvS_jG");
usuarioRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {     
        for (DataSnapshot child : dataSnapshot.getChildren()){
            Usuario franciscoJoao = child.getValue(Usuario.class);
            // Supostamente cada usuário com seu id
            // Se for usar algo como orderbyChild("idade").equalTo(18), não pare o loop
            break;
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) { }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.