0

im trying to validate whether a user input is valid, but seems like my method returns before actual completion. both OnCompleteListener and onSuccessListener has the same issue. Wondering if there's any way to solve this issue. Would separating return solve the issue...

public static boolean signUp_validateUsername(Context _context, String username){
        mContext = _context;

        mAuth = FirebaseAuth.getInstance();
        mAuth.fetchSignInMethodsForEmail(emailReformatter(username)).addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {
            @Override
            public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {
                if (task.getResult().getSignInMethods().isEmpty()){
                    mRetVal = true;
                    Log.i(TAG, "onComplete: ");
                }
                    
                else mRetVal = false;
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                mRetVal = false;
            }
        });

//        mAuth.fetchSignInMethodsForEmail(emailReformatter(username)).addOnSuccessListener(new OnSuccessListener<SignInMethodQueryResult>() {
//            @Override
//            public void onSuccess(SignInMethodQueryResult signInMethodQueryResult) {
//                Log.i(TAG, "onSuccess: ");
//                mRetVal = true;
//            }
//        }).addOnFailureListener(new OnFailureListener() {
//            @Override
//            public void onFailure(@NonNull Exception e) {
//                Log.i(TAG, "onFailure: ");
//                mRetVal = false;
//            }
//        });
        Log.i(TAG, "retval: " + mRetVal);
        return mRetVal;
    }

this is the log

I/DatabaseUtil.TAG: retval: false
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/System: Ignoring header X-Firebase-Locale because its value was null.
I/DatabaseUtil.TAG: onComplete: 
V/FA: Inactivity, disconnecting from the service

AND which is more proper to use in this situation? completeListener vs. successlistener ?

Sean LEE
  • 23
  • 4
  • callbacks occur at somepoint in the future, not in a line by line execution order, hence your method just returns immediately `false` the current value of `mRetVal` before it is mutated later in the sign in callbacks. Consider refactoring the flow control of this method by including a callback of its own in the method signature `Consumer` perhaps. Also it is bad practice to hold a strong reference to a `Context` - if this is not the application context it will likely be a memory leak, and in this case probably unnecessary, remove the assignment and keep the context local scope. – Mark Jan 20 '22 at 01:00
  • There is no way you can do that. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this article, [How to read data from Firebase Realtime Database using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5). – Alex Mamo Jan 20 '22 at 07:59

0 Answers0