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();
}