在事务中,如果数据不存在,我只想写入数据
DocumentReference callConnectionRef1 = firestore.collection("connectedCalls").document(callChannelModel.getUid());
firestore.runTransaction(new Transaction.Function < Void > () {
@Override
public Void apply(Transaction transaction) throws FirebaseFirestoreException {
DocumentSnapshot snapshot = transaction.get(callConnectionRef1);
Log.d(TAG, callChannelModel.getUid());
if (!snapshot.exists()) {
//my code
transaction.set(callConnectionRef1, model);
} else {
//do nothing
}
return null;
});
您可以在我的文档中看到引用是基于 uid 的,并且在我的日志中我正在打印 uid
因此,如果 uid 的数据不存在,我的日志仅打印一次,并且我在其他地方调用 transaction.set() ,它会继续显示数据已经存在的 uid 日志,如果我不调用 transaction.set( )
我怎样才能阻止它。
最佳答案
我在 Android 上也遇到过这种情况。事务执行 5 次尝试应用自身,然后才会调用 onFailure()
函数(即使您在 apply()
函数中抛出异常)。
但看起来这是预期的行为:
- 请参阅此处:https://googleapis.dev/python/firestore/latest/transaction.html
请注意,MAX_ATTEMPTS
默认为 5。 - 在此 github 问题中 https://github.com/firebase/firebase-js-sdk/issues/520有人请求添加“重试次数”选项。
关于javascript - Firestore如何停止事务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50735358/