Firebase Query realtime database does not return changed values in the database

1

I have a query in Firebase that should return user data to verify that it still has access and its level of access to the system, however when modifying the values the query continues to bring the previous values.

private void buscarUser(String matricula){
    ValueEventListener buscaUserListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(!dataSnapshot.exists()){
                Toast.makeText(BuscaUserPage.this,
                        "Usuário não encontrado.", Toast.LENGTH_LONG).show();
            }else {
                try {
                    Usuario userRecebido = null;
                    for (DataSnapshot dados : dataSnapshot.getChildren()) {
                        userRecebido = dados.getValue(Usuario.class);
                        idUser = dados.getKey();
                    }
                    atualizarTela(userRecebido);
                }catch (Exception e){
                    Toast.makeText(BuscaUserPage.this,
                            e.getMessage(), Toast.LENGTH_LONG).show();
                }

            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(BuscaUserPage.this,
                    "Ocorreu um erro: " + Objects.requireNonNull(databaseError),
                    Toast.LENGTH_LONG).show();
        }
    };


    Query consulta = referenciaFirebase.child("Usuarios").orderByChild("matricula").equalTo(matricula);
    consulta.addListenerForSingleValueEvent(buscaUserListener);
}

User class:

public class Usuario implements Serializable {

private String id;
private String email;
private String senha;
private String matricula;

private int nivelAcesso;
private boolean ativo;
}

The Active and active level values that are changed.

    
asked by anonymous 15.06.2018 / 16:47

1 answer

1

The problem really was connected to Offline Persistence from Firebase. I was using this code to enable persistence:

public class CustomApplication extends Application {
@Override
public void onCreate() {
    super.onCreate();
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
}}

Then replace this class with a singleton that I use in just a few activities where it's interesting to keep persistence offline.

public static DatabaseReference getReferenciaFirebaseOffLine(){
    if(referenciaFirebase == null){
        bancoFirebase = FirebaseDatabase.getInstance();
        bancoFirebase.setPersistenceEnabled(true);
        referenciaFirebase = bancoFirebase.getReference();
    }
    return referenciaFirebase;
}
    
21.06.2018 / 19:55