Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between isSuccessful() and isComplete() method

I am using Firebase in my android application.In my sign-up method I'm using 'createUserWithEmailAndPassword()' method.To check if my sign up process is successful or not I'm using 'isSuccessful' method.

 firebaseAuth.createUserWithEmailAndPassword(m,p).addOnCompleteListener(new 
 OnCompleteListener<AuthResult>() 
 {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
            if(task.isSuccessful())
             {
                //some message               
             }
            else
            {
                 //some other message
            }
  };

But every time I try to sign up,the message shows that it hasn't been successful.Then I use 'isComplete()' instead of 'isSuccessful' and then it worked fine.I also checked the Firebase dashboard to ensure if the sign up process is working fine and it is. Now I want to know if there is any major difference between these two methods and if there will be any further problem if I use 'isComplete()',because I have checked other tutorials where 'isSuccessful()' has been used. One more thing,for the same code 'isComplete()' is returning true and creating user but 'isSuccessful()' isn't. The complete code:

 public class RegisterActivity extends AppCompatActivity {
private EditText mail,pass,cPass;
private FirebaseAuth firebaseAuth;
private ImageButton reg;
private TextView warn;
private String a,b,c;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    mail=(EditText) findViewById(R.id.email);
    pass=(EditText) findViewById(R.id.pass);
    cPass=(EditText) findViewById(R.id.Cpass);
    reg=(ImageButton) findViewById(R.id.reg);
    firebaseAuth=FirebaseAuth.getInstance();
    progressDialog=new ProgressDialog(this);
    warn=(TextView) findViewById(R.id.warnTv);
    warn.setText("");
    reg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            a=mail.getText().toString().trim();
            b=pass.getText().toString().trim();
            c=cPass.getText().toString().trim();
            if(a.isEmpty() || b.isEmpty() || c.isEmpty())
            {
                Toast.makeText(getApplicationContext(),"all the fields are mandatory",Toast.LENGTH_SHORT).show();
            }
            else
            {
                if(LoginActivity.validate(a))
                {
                    if(b.equals(c))
                    {
                        progressDialog.setMessage("Wait a sec");
                        progressDialog.show();
                        firebaseAuth.createUserWithEmailAndPassword(a,b).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if(task.isSuccessful())
                                {
                                    progressDialog.dismiss();
                                    Toast.makeText(getApplicationContext(),"Registration successful",Toast.LENGTH_SHORT).show();
                                    startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
                                    finish();
                                }
                                else
                                {
                                    progressDialog.dismiss();
                                    Toast.makeText(getApplicationContext(),"Something went wrong",Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                    else {

                        warn.setText("Passwords in both the field must be same");
                    }
                }
                else
                {
                    warn.setText("Enter a valid emailId");
                }
            }
        }
    });

Please help me with its answer.Thanks in advance.

like image 950
Ankush Pandit Avatar asked Jan 16 '18 06:01

Ankush Pandit


1 Answers

A Task is "complete" when the work represented by the Task is finished, regardless of its success or failure. There may or may not have been an error, and you have to check for that.

A Task is "successful" when the work represented by the task is finished, as expected, with no errors.

like image 126
Doug Stevenson Avatar answered Oct 11 '22 00:10

Doug Stevenson