Firestore交易失败,并显示“交易失败所有重试". [英] Firestore transaction fails with "Transaction failed all retries"
问题描述
我正在运行一个非常简单的Firestore事务,该事务会在文件不存在之前先检查文件是否存在.
I'm running a very simple Firestore transaction which checks for the presence of a document, before writing to it if absent.
(用例正在注册用户名-如果尚未注册,则当前用户可以获取它)
(The use case is registering a username - if it's not already registered, the current user gets to grab it)
以下是相关的Flutter代码的片段:
Here's a snippet of the relevant Flutter code:
DocumentReference usernameDocRef =
Firestore.instance.collection(_USERNAMES).document(username);
await Firestore.instance.runTransaction((transaction) async {
var snapshot = await transaction.get(usernameDocRef);
if (!snapshot.exists) {
transaction.set(usernameDocRef, {
_UsernamesKey.userid: _user.id,
});
}
});
此操作失败,但出现异常事务重试所有重试失败".
This is failing with an exception "Transaction failed all retries".
基于 Firestore文档,可能会发生故障两个原因:
Based on the Firestore documentation, failure can occur for two reasons:
- 事务在写操作之后包含读操作.读取操作必须始终在任何写入操作之前进行.
- 该事务读取了在事务外部修改的文档.在这种情况下,事务将自动再次运行.交易被重试了有限次.
- The transaction contains read operations after write operations. Read operations must always come before any write operations.
- The transaction read a document that was modified outside of the transaction. In this case, the transaction automatically runs again. The transaction is retried a finite number of times.
我认为我不会触发任何一个.有什么建议吗?
I don't think I trigger either of those. Any suggestions?
推荐答案
最近已修复- https://github.com/flutter/plugins/pull/1206 .
如果您使用的是主频道,该修复程序应该已经可用.对于其他渠道(开发,测试版,稳定版)YMMV.
If you are using the master channel, the fix should be available already. For the other channels (dev, beta, stable) YMMV.
这篇关于Firestore交易失败,并显示“交易失败所有重试".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!