gpt4 book ai didi

android - 为什么此错误 : java. lang.IllegalStateException:在调用 commit() 后无法再使用写入批处理

转载 作者:行者123 更新时间:2023-12-05 00:20:08 25 4
gpt4 key购买 nike

以下代码我试图删除一个成员,应该删除一个成员及其详细信息和所有费用记录。

 public void deleteMember(Member member) {
final WriteBatch batch = FirebaseFirestore.getInstance().batch();
DocumentReference memberDoc = getMemberReference().document(member.getMemberId());
batch.delete(memberDoc);
getFeeReference().whereEqualTo("id", member.getMemberId())
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for(QueryDocumentSnapshot snapshot : queryDocumentSnapshots){
batch.delete(snapshot.getReference()); // this is the line of error
}
}
});
batch.commit();
getMemberImagesReference().child(member.getMemberId() + "t.jpg").delete();
getMemberImagesReference().child(member.getMemberId() + "f.jpg").delete();
}

但我收到以下错误
 2020-04-10 22:57:33.462 29454-29454/com.abhinav.chauhan.gymdatamanager D/AndroidRuntime: Shutting 
down VM
2020-04-10 22:57:33.464 29454-29454/com.abhinav.chauhan.gymdatamanager E/AndroidRuntime: FATAL
EXCEPTION: main
Process: com.abhinav.chauhan.gymdatamanager, PID: 29454
java.lang.IllegalStateException: A write batch can no longer be used after commit() has been called.
at com.google.firebase.firestore.WriteBatch.verifyNotCommitted(com.google.firebase:firebase-
firestore@@21.4.2:202)
at com.google.firebase.firestore.WriteBatch.delete(com.google.firebase:firebase-
firestore@@21.4.2:179)
at
com.abhinav.chauhan.gymdatamanager.database.FireBaseHandler$1.onSuccess(FireBaseHandler.java:110)
at
com.abhinav.chauhan.gymdatamanager.database.FireBaseHandler$1.onSuccess(FireBaseHandler.java:106)
at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

最佳答案

get()是异步的,并在查询完成之前立即返回。之后,您的代码将继续提交带有单个删除的批处理。一段时间后,在 get() 完成后,您的代码会在完成后尝试将另一个删除添加到批处理中。这就是您收到错误的原因 - 在您调用 commit() 后无法再次修改批处理.

也许你应该等到你的回调被调用后再添加所有要修改的文档并调用 commit(),以确保 commit()是对批处理调用的最后一个方法。

关于android - 为什么此错误 : java. lang.IllegalStateException:在调用 commit() 后无法再使用写入批处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61152781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com