你好,我有一个情况,我有一个自定义FirebaseAuthLiveData
类扩展Livedata
并实现OnCompleteListener
。这个类基本上与firebase进行所有与身份验证相关的工作。我正在关注应用程序的mvvm架构。所以我将观察活动中的liveata。现在的问题是当我尝试在Task Not Complete yet
中保存livingata的值时我得到oncomplete
异常但是当我再次重新打开应用程序时它已经登录了..我已经附加了这个类,如果有的话可以指出我做错了什么,
我正在使用的firebase版本是11.8.0
class FirebaseAuthLiveData (private val firebaseAuth: FirebaseAuth) : LiveData<Resource<FirebaseUser>>(), OnCompleteListener<AuthResult>, OnFailureListener{
override fun onFailure(p0: Exception) {
value = Resource.error(p0.localizedMessage,null)
}
override fun onComplete(p0: Task<AuthResult>) {
Log.d("firebase auth", "is task successful : "+p0.isSuccessful);
if (p0.isSuccessful) {
value = Resource.success(p0.result.user)
}
}
//this is called from a viewmodel
fun signInAnonymously( ){
firebaseAuth.signInAnonymously()
.addOnFailureListener(this)
.addOnCompleteListener(this)
}
}
这里是logcat
FATAL EXCEPTION: main
Process: com.creoit.android.withdrawalwarrior, PID: 10246
java.lang.IllegalStateException: Task is not yet complete
at com.google.android.gms.common.internal.zzbq.zza(Unknown Source)
at com.google.android.gms.tasks.zzn.zzbjk(Unknown Source)
at com.google.android.gms.tasks.zzn.getResult(Unknown Source)
at com.creoit.android.withdrawalwarrior.prior_info.PriorInfoContentActivity$signInAnonymouslyWithFirebase$1.onChanged(PriorInfoContentActivity.kt:124)
at com.creoit.android.withdrawalwarrior.prior_info.PriorInfoContentActivity$signInAnonymouslyWithFirebase$1.onChanged(PriorInfoContentActivity.kt:33)
at android.arch.lifecycle.LiveData.considerNotify(LiveData.java:131)
at android.arch.lifecycle.LiveData.dispatchingValue(LiveData.java:148)
at android.arch.lifecycle.LiveData.setValue(LiveData.java:294)
at com.creoit.android.withdrawalwarrior.firebase.livedata.FirebaseAuthLiveData.onComplete(FirebaseAuthLiveData.kt:28)
at com.google.android.gms.tasks.zzf.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
Firebase,请检查您是否具有访问Firebase的权限以匿名登录。 为此,请转到Firebase身份验证/登录方法,然后启用“匿名登录”切换按钮。
答案 1 :(得分:0)
放入while((!uri.isComplete()));像这样。
reference.putFile(file).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
toast("Entered success listner");
Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
while ((!uri.isComplete()));
Uri url = uri.getResult();
databaseReference = FirebaseDatabase.getInstance().getReference("uploads");
PDFDoc uploadpdf = new PDFDoc(name.getText().toString(), url.toString());
databaseReference.child(databaseReference.push().getKey()).setValue(uploadpdf);
toast("Uploaded to database");
progressDialog.dismiss();
}
})
这对我有用:)