samedi 26 octobre 2019

Both my if and else statements in datasnapshot are being executed

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

     DatabaseReference databaseReference=FirebaseDatabase.getInstance().getReference("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

Aucun commentaire:

Enregistrer un commentaire