Reda M Reda M - 4 years ago 162
Android Question

Wait until object is not null to execute my if condition Java

I have two methods, both using the same object.

Object A;

public void methodA() {
//Client code talks to server, create data, and instantiate A.
A = new A();
}

public void methodB() {
if(A!=null) {
//do something
}
}


This is a simplified logic to what I'm trying to achieve, but at the moment I'm instantiating Object A in method A, my app is talking to the server and creating data for that object and when my app is executing methodB(), ObjectA is null.

Another catch is that when I'm debugging my app, the ObjectA has an instance and methodB() is executed properly because debugging is a slower process, however, when I'm running it, ObjectA is returning as null. I tried to use notify and wait, but I'm not able to set my objectA in a synchronized block because it's returning an null so my app is crashing.




EDIT: Adding my current code:

//Firebase Authentication
private FirebaseAuth mAuth;
//Database reference
private DatabaseReference databaseReference;

public void methodA() {
mAuth.createUserWithEmailAndPassword(
getUserEmailAddress(), getUserPassword())
.addOnCompleteListener(ActivityA.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "createUserWithEmailAndPassword: onComplete" + task.isSuccessful());
if (!task.isSuccessful()) {
Toast.makeText(ActivityA.this, "authentication failed",
Toast.LENGTH_LONG).show();
}
}
});
}

public void methodB() {
if (mAuth.getCurrentUser() != null) {
databaseReference.child(mAuth.getCurrentUser().getUid()).setValue(userInformation);
}
}


mAuth.getCurrentUser() is returning null because methodB is executing before the server is creating a firebaseUser instance.

Answer Source

After seeing your actual situation, i believe the wait/notify method shown in other answers isn't the best solution for your case.

It seems the fundamental problem you have here is that you need to perform some action only once a user is signed in/authenticated. This is a classic case of attempting to perform a synchronous action with an object that you obtain in an asynchronous manner. The general best practice for those situations is only performing the action once you get a result from your async action, using a callback. A great solution here is to use the callbacks that Firebase provides for change of authentication state. From the docs:

mAuthListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser user = firebaseAuth.getCurrentUser();
        if (user != null) {
            // User is signed in
            Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
        } else {
            // User is signed out
            Log.d(TAG, "onAuthStateChanged:signed_out");
        }
        // ...
    }
};

From here you can determine your logic for calling methodB (depends on your specific use case).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download