0

I am stuck with the following issue. I tried searching for answers but none of the responses could help me so i am asking this question here. Any response will be greatly appreciated!

I am using this below AsyncTask to use the login methods exposed by the Firebase API. However when i invoke new LoginOperation().execute() at the click of login button, i am not seeing the expected results. I am pasting the code below and the Logcat output.

I would like to know why onPostExecute is getting executed before doInBackground? Please note i am using a valid email id & password, so i should have been able to login properly.

Code:

private class LoginOperation extends AsyncTask<Void, Void, Boolean> {


    protected Boolean doInBackground(Void... params) {
        try{
            authClient.loginWithEmail(emailid.getText().toString(),password.getText().toString(), new SimpleLoginAuthenticatedHandler() {
                public void authenticated(
                        com.firebase.simplelogin.enums.Error error, User user) {
                     if(error != null) {
                          // There was an error logging into this account
                          loginStatus=false;
                          errorMsg=error.name();
                          Log.d(appName, "Inside if block  in doInBackground of LoginOperation");
                        }
                        else {
                          // We are now logged in
                            loginStatus=true;
                            Log.d(appName, "Inside else block  in doInBackground of LoginOperation");
                        }

                }
                });
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return Boolean.valueOf(loginStatus);
    }


    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        if (result.booleanValue()) {
            toastMsg="User logged in successfully";               
            Log.d(appName, "Inside onPostExecute  success of LoginOperation");

          }
        else
        {
           toastMsg="Error in login";
           Log.d(appName, "Inside onPostExecute failure of LoginOperation");

        }

         TextView displayStatus = (TextView) findViewById(R.id.displayStatus);
         displayStatus.setText(toastMsg); 

    }


    protected void onPreExecute() {
        super.onPreExecute();
    }


    protected void onProgressUpdate(Void... values) {}
}

Code invoked on Login click:

public void onLogin(View arg0) 
{
Log.d(appName, " email id is " + emailid.getText().toString());
Log.d(appName, " password is " + password.getText().toString());

try {
 Boolean finalStatus= new LoginOperation().execute().get(5000, TimeUnit.MILLISECONDS);
 Log.d(appName, " final Status is: " + finalStatus.booleanValue());
 } 

LogCat:

01-27 17:50:02.054: D/LOGIN(984):  email id is abc@gmail.com
01-27 17:50:02.054: D/LOGIN(984):  password is abc123
01-27 17:50:02.082: D/LOGIN(984):  final Status is: false
01-27 17:50:02.082: D/LOGIN(984): Inside onPostExecute failure of LoginOperation
01-27 17:50:05.502: D/LOGIN(984): Inside else block  in doInBackground of LoginOperation

Expected result:

Inside else block  in doInBackground of LoginOperation
Inside onPostExecute  success of LoginOperation
Saggy
  • 624
  • 8
  • 23
user3199806
  • 13
  • 1
  • 1
  • 4

3 Answers3

1

Most likely this is because SimpleLoginAuthenticatedHandler.authenticated() is executed asynchronously. It looks you don't need AsyncTask at all.

sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
  • Thank you for your suggestion. I removed the Async Task and followed the suggestion given by FD_ below and it worked. Thank you for your help. – user3199806 Jan 27 '14 at 21:44
1

My guess is that loginWithEmail() runs within a Thread. That means that when you call this method, the thread is indeed run but that doesn't mean it has ended its execution, so maybe it's running paralelly, you get back, finish the doInBackground() method and it joins the onPostExecute() method.

I would remove the AsyncTask if it's that way, it's not necessary.

nKn
  • 13,691
  • 9
  • 45
  • 62
  • Thank you for your help. Yes, loginWithEmail runs within a Thread. So i removed AsyncTask and followed FD_ suggestion and was able to run my code properly. – user3199806 Jan 27 '14 at 21:46
1

You don't need your AsyncTask because the class you are using in doInBackground seems to already perform its task in a background thread. Move the code in doInBackground somewhere to the UI thread and move your code from onPostExecute into the SimpleLoginAuthenticationHandler.

Currently, onPostExecuted is called immediately, because it just starts a new background thread and comes back immediately.

FD_
  • 12,947
  • 4
  • 35
  • 62
  • Thank you so much for this suggestion! I followed your steps and i am getting the desired output now. Thanks a ton for your help. – user3199806 Jan 27 '14 at 21:43
  • I know this is two years old but can anyone tell how @FD_ would have known the class inside doInBackground is already on another background thread? Can you tell just from reading the above code? – Gil Jan 31 '17 at 09:00
  • @Gil I think it was just an educated guess based on the fact that a handler is passed to the method that's being called there. – FD_ Jan 31 '17 at 13:11