dimanche 27 octobre 2019

Why are Both if and else statements executed in datasnapshot?

I am checking for a child in the firebase database using the code below

   DatabaseReference 
 databaseReference=FirebaseDatabase.getInstance().get 
  Reference().child.("cook").child(userID);
   databaseReference.addValueEventListener(new 
   ValueEventListener() {
   @Override
    public void onDataChange(@NonNull DataSnapshot 
    dataSnapshot) {

    if(dataSnapshot.exists()){

        Toast.makeText(getApplicationContext()," child is present ",Toast.LENGTH_LONG).show();


      }else {
  try {
            logins();
        } catch (JSONException e) {

          Toast.makeText(getApplicationContext(), "error,  ensure you have internet connection", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
        }
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}

});

If the child is present a toast is made child is present, if not the logins() method is called however, both the the toast and method logins() is called. The toast happens first and later on the method is called

Aucun commentaire:

Enregistrer un commentaire