我在Android上使用Firebase来注册用户并将其数据插入到数据库中。 我有3个类,片段类从用户获取所有数据 - >将对象发送到Model类 - > UserModelFirebase
这是项目的层次结构。
这是代码:
SignUpFragment
public void SignUp(){
User newUser = new User(txtEmail.getText().toString(),
txtPassword.getText().toString(),
false);
String password = txtPassword.getText().toString();
Model.instance.addUser(newUser, password, new Model.IAddUser() {
@Override
public void onComplete(User user) {
MusicPostListFragment listFragment = MusicPostListFragment.newInstance(1);
onButtonPressed(listFragment);
}
@Override
public void onError(String reason) {
Toast.makeText(v.getContext(), "Unable to create user, Please try again", Toast.LENGTH_LONG);
}
});
}
模型
public class Model {
private ModelUserFirebase modelUserFirebase;
public final static Model instance = new Model();
private Model(){
modelUserFirebase = new ModelUserFirebase();
}
public interface IAddUser {
void onComplete(User user);
void onError(String reason);
}
public void addUser(User user, String password, final IAddUser callback ) {
modelUserFirebase.addUser(user, password, new ModelUserFirebase.IAddUser() {
@Override
public void onComplete(User user) {
callback.onComplete(user);
}
@Override
public void onError(String reason) {
callback.onError(reason);
}
});
}
UserModelFirebase
public class ModelUserFirebase {
private static final String USERS_KEY = "Users";
private FirebaseAuth firebaseAuth;
private FirebaseDatabase database;
private DatabaseReference usersReference;
private FirebaseAuth.AuthStateListener mAuthStateListener;
private User currentUser;
public ModelUserFirebase() {
firebaseAuth = FirebaseAuth.getInstance();
database = FirebaseDatabase.getInstance();
usersReference = database.getReference(USERS_KEY);
}
interface IAddUser {
void onComplete(User user);
void onError(String reason);
}
public void addUser(final User user, String password, final IAddUser callback) {
firebaseAuth.createUserWithEmailAndPassword(user.getEmail(), "123456Ee!")
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("dev","createUserWithEmail: onComplete()");
FirebaseUser firebaseUser = task.isSuccessful() ?
firebaseAuth.getCurrentUser() : null;
if (firebaseUser != null) {
User savedUser = new User(user);
savedUser.setId(firebaseUser.getUid());
usersReference.child(firebaseUser.getUid()).setValue(savedUser);
callback.onComplete(savedUser);
}
else {
callback.onError(task.getException().getMessage());
}
}
});
}
发生了什么事? 当我调试代码时,我看到addOnCompleteListener行之后 什么都没发生,也没有onComplete onError