【发布时间】:2018-11-17 00:32:14
【问题描述】:
在事务中我只想在数据不存在时写入数据
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( )
我怎样才能阻止它。
【问题讨论】:
-
您能否详细说明“继续运行”的含义?
runTransaction根本不回来吗? -
我添加了事务开始的日志......它一直显示该日志
-
请问您能否在问题中提供更多详细信息?目前我不清楚你的意思。如果您能提供minimal reproducible example,那将非常有帮助。
-
我编辑了我的问题...请看一下
-
我的猜测是一个异常单独发生。同样,如果您可以将您的问题编辑为minimal reproducible example,这将使某人更容易重现该问题,那将非常有帮助。 (示例应该什么都不做但演示问题。)
标签: javascript firebase google-cloud-firestore