OliverDamon
OliverDamon

Reputation: 147

How to create a condition if there is the same value in firebase

How can I see if there is an equal value in a child and not save if it exists.

I tried doing this here but it did not work: How can I check if a value exists already in a Firebase data class Android

enter image description here

FirebaseAuth autenticacao = ConfiguracaoFirebase.getFirebaseAutenticacao();
final String idUsuario = CustomBase64.codificarBase64(autenticacao.getCurrentUser().getEmail());
DatabaseReference firebase = ConfiguracaoFirebase.getFirebaseDatabase().child("historico").child(idUsuario);
firebase.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.hasChild("id")) {
           Toast.makeText(getActivity(), "Exist", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getActivity(), "No Exist", Toast.LENGTH_LONG).show();

        }
    }

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

Upvotes: 2

Views: 161

Answers (2)

zbys
zbys

Reputation: 463

Create yourself another function that pushes the data to firebase and call it from inside your listener. (in this example, create the function pushValueToFirebase)

FirebaseAuth autenticacao = ConfiguracaoFirebase.getFirebaseAutenticacao();
final String idUsuario = CustomBase64.codificarBase64(autenticacao.getCurrentUser().getEmail());
DatabaseReference firebase = ConfiguracaoFirebase.getFirebaseDatabase().child("historico");
firebase.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Boolean exists = false;
        for (DataSnapshot child : dataSnapshot.getChildren()) {
            if (child.getKey().equals(idUsario) {
                exists = true;
            }
        }
        if (!exists) {
             //Your code here to push idUsario
        }
    }

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

Upvotes: 2

Peter Haddad
Peter Haddad

Reputation: 80914

Try this:

FirebaseAuth autenticacao = ConfiguracaoFirebase.getFirebaseAutenticacao();
                    final String idUsuario = CustomBase64.codificarBase64(autenticacao.getCurrentUser().getEmail());
                    DatabaseReference firebase = ConfiguracaoFirebase.getFirebaseDatabase().child("historico").child(idUsuario).child("id");
                    firebase.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if (dataSnapshot.exists()) {
                               Toast.makeText(getActivity(), "Exist", Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(getActivity(), "No Exist", Toast.LENGTH_LONG).show();

                            }
                        }

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

Add the child id to the reference above child("historico").child(idUsuario).child("id") then use exists() to check if this dataSnapshot is in your database.

Upvotes: 1

Related Questions