我有一个颤动的应用程序,我的客户已经使用了近两个月在Android上,现在我也必须制作iOS版本。
当我尝试使用userAccount.id运行get调用时,我从firebase返回数据6次,但后来我收到“Transaction failed all retries”错误。
我尝试了一些我能在谷歌上找到的东西。
注意:来自firebase的流数据正常工作
我正在使用cloud_firestore flutter包
这是功能:
static Future<WebRequestResponse> getUserAccountByID(String id) async {
if (!await isInternetConnectionAvailable()) {
return WebRequestResponse(false, null, null,
errorType: ErrorType.ERROR_NO_INTERNET_CONNECTION);
}
DocumentSnapshot ds;
final TransactionHandler getTransaction = (Transaction tx) async {
ds = await tx.get(userAccountsCollection.document(id));
print(ds.data);
return ds.data;
};
WebRequestResponse response = WebRequestResponse(false, null, null);
return Firestore.instance.runTransaction(getTransaction).then((jsonData) {
print(jsonData);
UserAccount userAccount = UserAccount.fromJson(jsonData);
print(userAccount.id);
if (userAccount.id != null) {
response.success = true;
response.data = userAccount;
return response;
} else {
response.success = false;
response.errorType = ErrorType.ERROR_USER_NOT_FOUND;
return response;
}
}).catchError((error) {
print('get_user_account_by_id_error: $error');
response.success = false;
response.error = error;
response.errorType = ErrorType.ERROR_UNKNOWN;
return response;
});
}
这是我得到的回应:
flutter: {id: Edaavf39l4Y3cbfGy4RblYSdy1p1, profilePictureURL: https://firebasestorage.googleapis.com/v0/b/fr<REDACTED REST OF JSON>}
flutter: {id: Edaavf39l4Y3cbfGy4RblYSdy1p1, profilePictureURL: https://firebasestorage.googleapis.com/v0/b/fr<REDACTED REST OF JSON>}
flutter: {id: Edaavf39l4Y3cbfGy4RblYSdy1p1, profilePictureURL: https://firebasestorage.googleapis.com/v0/b/fr<REDACTED REST OF JSON>}
flutter: {id: Edaavf39l4Y3cbfGy4RblYSdy1p1, profilePictureURL: https://firebasestorage.googleapis.com/v0/b/fr<REDACTED REST OF JSON>}
flutter: {id: Edaavf39l4Y3cbfGy4RblYSdy1p1, profilePictureURL: https://firebasestorage.googleapis.com/v0/b/fr<REDACTED REST OF JSON>}
flutter: get_user_account_by_id_error: PlatformException(9, Transaction failed all retries., null)
正如您所看到的那样,数据是通过事务内部的打印正确打印出来的,但它发生了6次,之后就失败了。知道为什么会这样,以及如何解决它?
非常感谢你
因此经过几周的挣扎和尝试随机的事情,我认为可能是因为我在虚拟机中运行(无法确认)。
无论如何,我的解决方案是,在每次tx.get调用后,我必须添加一个具有相同文档ID的tx.set调用,并使用空数据进行更新。我不知道为什么,或者它是如何工作的,但确实如此。我虽然回答以防将来遇到同样的问题。