theGuy05 theGuy05 - 1 month ago 12
Android Question

How can I use a Firebase Query to return a boolean?

I am trying to make a method called sendRegisterUserMessage() that sends a User object to the Firebase db if the User does not already exist within the database. For some reason the method always returns false despite finding the object in the database with the listener.

Anyways I invoke this method in my MainActiviy in onCreate():

MessageSenderHandler.getInstance().sendRegisterUserMessage();


In my MessageSenderHandler class:

//Checks if user is Registered- if not it sends a RegisterUser message
public void sendRegisterUserMessage() {
System.out.println("registered: " +DatabaseManager.getInstance().isCurrentUserRegistered()); //This prints false no matter what
if (DatabaseManager.getInstance().isCurrentUserRegistered() == false) {
FirebaseDatabase.getInstance().getReference().child(MessageTypes.REGISTER_USER_MESSAGE).child(
CurrentUser.getInstance().getUserId())
.setValue(
new RegisterUserMessage()
);
}
}


Then here is my isCurrentUserRegistered() method in my DatabaseManager class

public boolean isCurrentUserRegistered() {
Query query = databaseReference.child("/" + MessageTypes.REGISTER_USER_MESSAGE + "/" + CurrentUser.getInstance().getUserId());
query.addValueEventListener(isUserRegisteredDataListener);
System.out.println("REGISTERED:" + isUserRegisteredDataListener.isUserRegisterd()); //This also prints false no matter what
return isUserRegisteredDataListener.isUserRegisterd();
}


Here is my IsUerRegisteredDataListener

public class IsUserRegisteredDataListener implements ValueEventListener {

static boolean exists;

private static IsUserRegisteredDataListener isUserRegisteredDataListener;
public IsUserRegisteredDataListener() {
isUserRegisteredDataListener = this;
}
public static IsUserRegisteredDataListener getInstance() {
if (isUserRegisteredDataListener == null) {
isUserRegisteredDataListener = new IsUserRegisteredDataListener();
}
return isUserRegisteredDataListener;
}
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

this.exists = dataSnapshot.exists();
System.out.println("EXISTS" + exists)); //This prints true
}

@Override
public void onCancelled(DatabaseError databaseError) {

}

public boolean isUserRegisterd() {
return exists;
}


}

I don't understand why the first two methods print FALSE but the one with the actual data listener prints TRUE inside the listener

Answer
public boolean isCurrentUserRegistered() {
    Query query = databaseReference.child("/" + MessageTypes.REGISTER_USER_MESSAGE + "/" + CurrentUser.getInstance().getUserId());
    query.addValueEventListener(isUserRegisteredDataListener);
    System.out.println("REGISTERED:" + isUserRegisteredDataListener.isUserRegisterd()); //This also prints false no matter what
    return isUserRegisteredDataListener.isUserRegisterd();
}

In this method, you have to wait for firebase result to get isUserRegistered. So your IsUserRegisteredDataListener class can not work what you wish.

Can you show where you initialize isUserRegisteredDataListener field?