Firebase Query Database does not return changed values in the database

Asked

Viewed 544 times

1

I have a query in Firebase that should return the user data to check if he still has access and his level of access to the system, however when modifying the values the query continues bringing 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 level and active values that are changed.

  • You have activated Firebase’s Offline Persistence?

  • Offline persistence is active.

  • That’s why you’re having this problem. It may be that it is taking too long to load the new values, or there may be a problem with your connection. This problem is common when using offline persistence.

1 answer

1


The problem was actually connected to Firebase’s Offline Persistence. It was using this code to activate 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 is interesting to keep the persistence off line.

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

Browser other questions tagged

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