2

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) {
    }
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
OliverDamon
  • 147
  • 9

2 Answers2

2

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) {
    }
});
zbys
  • 453
  • 2
  • 8
  • I did not understand. Could you give an example? – OliverDamon Aug 02 '18 at 19:48
  • Well you use the listener to check if the value exists, then if it does call a function that pushes that value. Are you asking specifically how to add a value to firebase? – zbys Aug 02 '18 at 19:51
  • This id is a value. Every time I add another, it creates another, looks http://prntscr.com/kdyx57 – OliverDamon Aug 02 '18 at 19:51
  • and what is your desired behavior? – zbys Aug 02 '18 at 19:57
  • I need to check if any value has already been sent with the same id. If you already have I will not save. – OliverDamon Aug 02 '18 at 19:58
  • remove .child(idUsario) when getting the reference, then in the listener, iterate over dataSnapshot.getChildren() and check if any of them have the key (or value, depending on your case) of idUsario. If none of them do, then write the data – zbys Aug 02 '18 at 20:05
  • Could you edit your post? It gets easier. And also if it works I mark it as completed. – OliverDamon Aug 02 '18 at 20:08
  • I edited it, do that and copy paste the stuff you have in your pastbin where you're supposed to push the values to firebase. – zbys Aug 02 '18 at 20:12
  • Ok so I shouldn't have to use google translate on your questions, you can do that yourself, but if he doesn't save it, so if exists is true, just display a Toast like you were doing. – zbys Aug 02 '18 at 20:25
1

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.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • This is not working for me. He is creating another key. – OliverDamon Aug 02 '18 at 19:47
  • In the answer it checks if you have `id` under this `idUsuario` – Peter Haddad Aug 02 '18 at 19:49
  • This id is a value. Every time I add another, it creates another, looks http://prntscr.com/kdyx57 – OliverDamon Aug 02 '18 at 19:51
  • then add a query if you want the value to not be added again. Do this `DatabaseReference firebase = ConfiguracaoFirebase.getFirebaseDatabase().child("historico").orderByChild("id").equalTo(the_id_added);` and datasnapshot.exists() will check if this id value already exists in the database. – Peter Haddad Aug 02 '18 at 21:05