【发布时间】:2019-09-29 12:00:24
【问题描述】:
Firestore Transactions 给我以下错误:
原因:com.google.firebase.firestore.FirebaseFirestoreException:事务中读取的每个文档也必须写入。
这是一段代码:
db.runTransaction(new Transaction.Function<Void>() {
@Override
public Void apply(Transaction transaction) throws FirebaseFirestoreException {
DocumentReference docRef2 = db.collection("ABC").document(mMatchedUserId);
DocumentReference ref3 = db.collection("XYZ").document(mCurrentUserId);
DocumentReference ref4 = db.collection("XYZ").document(mMatchedUserId);
DocumentSnapshot documentSnapshot2 = transaction.get(docRef2);
if(documentSnapshot2.exists())
{
transaction.delete(docRef2);
transaction.set(ref3, myMap1);
transaction.set(ref4,myMap2);
}
return null;
}
}).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "Transaction success!");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Transaction failure.", e);
}
});
如果我删除这个 if 条件 :if(documentSnapshot1.exists() && documentSnapshot2.exists()) 则事务成功完成并且没有错误。
但设置此事务的主要点是 if 条件。请帮忙。
【问题讨论】:
标签: java android firebase google-cloud-firestore