【发布时间】:2016-06-07 11:42:54
【问题描述】:
createUserWithEmailAndPassword 返回的对象是Task<AuthResult>,我可以为其设置onSuccess 或onFailure 回调;但主要问题是,我需要这里的主线程 wait 完成此过程,然后再进行下一步。创建部分 工作,但是当线程最终完成时,Task 对象说成功为假并返回,这也让我感到困惑,因为它确实成功地创建了用户。
if (firebaseAuth.getCurrentUser() == null) {
final Task<AuthResult> resultTask = firebaseAuth.createUserWithEmailAndPassword(
"email@address.com",
"tester1234");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while(!resultTask.isComplete()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
thread.join(10000);
if(!resultTask.isSuccessful())
return false;
}
在下面尝试这种方法只会无限期地挂起:
if (firebaseAuth.getCurrentUser() == null) {
final CountDownLatch countDownLatch = new CountDownLatch(1);
firebaseAuth.createUserWithEmailAndPassword(
"email@address.com",
"test1234")
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
countDownLatch.countDown();
}
});
countDownLatch.await();
}
【问题讨论】:
-
你在哪里启动/执行你的任务?
-
如果你不启动你的任务,它确实永远不会完成,有意义的 WDYT?
-
@NicolasFilotto 任务由
createUserWithEmailAndPassword启动 -
如果是这样,请向我们展示 createUserWithEmailAndPassword 的内容,因为到目前为止我们所拥有的没有帮助
-
@NicolasFilotto 这里是我没有深入研究 Firebase 代码developers.google.com/android/reference/com/google/firebase/…
标签: java multithreading android-asynctask firebase firebase-authentication