0

I work in an android app and I got a problem that I can't solve, I tried searching on google and do it in another way but I'm stuck. Here's my code:

private FirebaseAuth mAuth;
private FirebaseFirestore db;
private Boolean complete=false;

public boolean createUser(final String prenom, final String nom, final String mail, final String pass) {
       RegisterActivity ra = new RegisterActivity();
       mAuth = FirebaseAuth.getInstance();
       db = FirebaseFirestore.getInstance();
       mAuth.createUserWithEmailAndPassword(mail, pass).addOnCompleteListener(ra, new OnCompleteListener<AuthResult>() {
             @Override
             public void onComplete(@NonNull Task<AuthResult> task) {
                    mAuth.getCurrentUser().sendEmailVerification();
                    Map<String, Object> user = new HashMap<>();
                    user.put("Mail", mail);
                    user.put("Nom", nom);
                    user.put("Prenom", prenom);
                    user.put("Rang", "Aucun");
                    user.put("Valide", false);
                    db.collection("users").document(mAuth.getCurrentUser().getUid()).set(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            complete=true;
                            Log.w("RESULT",complete.toString());
                        }
                    });
                }
        });
  Log.w("RESULT",complete.toString());
  return complete;
}

I don't know why my function return false, the log says in first "RESULT false" and after "RESULT true". What's wrong ?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Off
  • 1
  • You can't return something that hasn't loaded yet. Any code that requires data from the database must be inside `onComplete` or be called from there. See my answer here for examples: https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Oct 13 '19 at 19:55
  • I tried to add my own callback but it doesn't work, I also tried to wait until the query but it's not recommended. How can I return when the task is complete? – Off Oct 13 '19 at 21:22
  • "it doesn't work" -> Please update your question to include [the minimal code that reproduces this new problem](http://stackoverflow.com/help/mcve). – Frank van Puffelen Oct 14 '19 at 01:48
  • You can't reliable make your code "wait" until the task is complete. My linked answer (and the other questions linked from there) should make that pretty clear. – Frank van Puffelen Oct 14 '19 at 01:49

0 Answers0