Issue
I have created a AsyncTask
for handling the Sing in. The issue is that Firebase method signInWithEmailAndPassword
is completed after the doInBackground
has returned value. I wan't my onPostExecute(Boolean task)
to respond to whether the login task was successful, but when I check the log messages i can see that onPostExecute()
finishes before onComplete()
.
How can i make my onPostExecute()
after the onComplete()
and doInBackground()
have finished.
The Log messages:
01-07 12:25:43.775 4046-4241/mk.capitalria.jusufi.riaposdeveloper V/SignInActivity: doInBackground() return type is false
01-07 12:25:43.785 4046-4046/mk.capitalria.jusufi.riaposdeveloper V/SignInActivity: onPostExecute() task is false
01-07 12:25:44.965 4046-4046/mk.capitalria.jusufi.riaposdeveloper V/SignInActivity: onComplete task is successful: true
01-07 12:25:44.965 4046-4046/mk.capitalria.jusufi.riaposdeveloper V/SignInActivity: onComplete task is complete: true
The AsyncTask class:
public class LoginTask extends AsyncTask<String, Void, Boolean> {
boolean isSuccessful;
boolean isComplete;
FirebaseAuth firebaseAuth;
@Override
protected void onPreExecute() {
super.onPreExecute();
firebaseAuth = FirebaseAuth.getInstance();
signInLayout.setVisibility(View.GONE);
sinInTask.setVisibility(View.VISIBLE);
}
@Override
protected Boolean doInBackground(String... strings) {
final String email = strings[0];
final String password = strings[1];
firebaseAuth.signInWithEmailAndPassword(email, password).
addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
isSuccessful = task.isSuccessful();
isComplete = task.isComplete();
Log.v(TAG, "onComplete task is successful: " + isSuccessful);
Log.v(TAG, "onComplete task is complete: " + isComplete);
}
});
Log.v(TAG, "doInBackground() return type is " + (isSuccessful && isComplete));
return isComplete && isSuccessful;
}
@Override
protected void onPostExecute(Boolean task) {
super.onPostExecute(task);
Log.v(TAG, "onPostExcecute task is: " + task);
if (!task) {
sinInTask.setVisibility(View.GONE);
signInLayout.setVisibility(View.VISIBLE);
Toast.makeText(SignInActivity.this, "Authentication failed.",
Toast.LENGTH_LONG).show();
} else {
Intent intent = new Intent(SignInActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
}
Solution
As was said, you really don't need to use AsyncTask here cause firebaseAuth.signInWithEmailAndPassword()
is non-blocking method. It means that your code is executed not step-by-step but in 2 different Thread
s. That's why you getting onPostExecute
first and after when response from Firebase
comes it prints onComplete
to logs.
If you really want to use AsyncTask
you have to wait while signInWithEmailAndPassword
returns some value and it can be done in following way.
public class LoginTask extends AsyncTask<String, Void, Boolean> {
boolean isSuccessful;
boolean isComplete;
FirebaseAuth firebaseAuth;
final ReentrantLock loginLock = new ReentrantLock();
...
@Override
protected Boolean doInBackground(String... strings) {
...
firebaseAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
isSuccessful = task.isSuccessful();
isComplete = task.isComplete();
Log.v(TAG, "onComplete task is successful: " + isSuccessful);
Log.v(TAG, "onComplete task is complete: " + isComplete);
loginLock.unlock()
}
})
.addOnFailureListener(new OnFailureListener {
@Override
public void onFailure(Exception e) {
loginLock.unlock();
}
});
loginLock.lock();
Log.v(TAG, "doInBackground() return type is " + (isSuccessful && isComplete));
return isComplete && isSuccessful;
}
...
}
Answered By - hluhovskyi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.