gpt4 book ai didi

java - 同时在 firebase 中添加多个条目

转载 作者:行者123 更新时间:2023-11-29 23:28:27 24 4
gpt4 key购买 nike

我必须使用 firebase 数据库创建订单。

我需要在整体操作中执行以下步骤

1- Update the balance of user 
2- Create a transaction with payment data
3- Create a record of order in orders listing
4- Update entries of local store.

现在所有这些细节都必须一起执行。在 firebase 中执行此操作的最佳方法是什么。要么

ref1.setValue(
ref2.setValue(
ref3.setValue(
ref4.setValue(

或通过交易,如果是的话如何?

最佳答案

根据您的评论:

The entires are not children of users, each is from different list. users, orders, transactions and stores

我建议您通过一次调用 DatabaseReference 的 updateChildren() 对 JSON 树中的多个位置执行同步更新。方法。

offical documentation 中的示例会是:

private void writeNewPost(String userId, String username, String title, String body) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
String key = mDatabase.child("posts").push().getKey();
Post post = new Post(userId, username, title, body);
Map<String, Object> postValues = post.toMap();

Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/posts/" + key, postValues);
childUpdates.put("/user-posts/" + userId + "/" + key, postValues);

mDatabase.updateChildren(childUpdates);
}

This example uses push() to create a post in the node containing posts for all users at /posts/$postid and simultaneously retrieve the key with getKey(). The key can then be used to create a second entry in the user's posts at /user-posts/$userid/$postid.

以这种方式进行的同步更新是原子的:要么所有更新都成功,要么所有更新都失败。

关于java - 同时在 firebase 中添加多个条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53120351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com