0

I want to check if there exists an key with a specific email. If it exists, I need to fetch the value (unique id for user in my application) for the key and check whether the id entered in application matches the id fetched from database. But the thing is my onDataChange() is responding after the I perform the check for equality. What could be done?

rootRef.child("shortlisted").child(email1).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NotNull DataSnapshot snapshot) {
            try {
                if (snapshot.getValue() != null) {
                    try {

                        aac = snapshot.getValue().toString();
                        System.out.println("STRINGAAC"+aac);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    //user doesnt exist
                    Log.e("TAG", " it's null.");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

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

        }
    });
    System.out.println("STRINGAAC"+aac);
    if(aac.equals("not found"))
    {
        Toast.makeText(globalContext,"not found", Toast.LENGTH_SHORT).show();
    }
    if(aac.equals(aacid_s))
    {
        Toast.makeText(globalContext,"Key Matched value", Toast.LENGTH_SHORT).show();
    }
    else
    {
        Toast.makeText(globalContext,"Key un-Matched value", Toast.LENGTH_SHORT).show();
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

1

This is a normal behavior since firebase API is asynchronous, which basically means when you run the above code, the code after onDataChange() will be called even before retrieving the full data. Therefore to solve this you have to do the following:

rootRef.child("shortlisted").child(email1).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NotNull DataSnapshot snapshot) {
            try {
                if (snapshot.getValue() != null) {
                    try {

                        aac = snapshot.getValue().toString();
                        System.out.println("STRINGAAC"+aac);
                        if(aac.equals("not found"))
                        {
                         Toast.makeText(globalContext,"not found", Toast.LENGTH_SHORT).show();
                        }
                        if(aac.equals(aacid_s))
                        {
                         Toast.makeText(globalContext,"Key Matched value", Toast.LENGTH_SHORT).show();
                        }
                        else
                        {
                         Toast.makeText(globalContext,"Key un-Matched value", Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    //user doesnt exist
                    Log.e("TAG", " it's null.");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

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

        }
    });
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Actually I want further processing so I don't need any toast messages. They were only for debugging. Anyway I can do the same using flag variables, right? – Aashish Chandra Jan 30 '21 at 10:50
  • If I try to do the same replacing the toast message line with a flag varaible, I again am facing the same issue! – Aashish Chandra Jan 30 '21 at 13:16
  • I don't want a toast, I want to proceed or stop further registration based on the value in my flag variable. Please help! – Aashish Chandra Jan 30 '21 at 13:17