0

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

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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Yayayaya
  • 216
  • 3
  • 13

0 Answers0