Java 同时在firebase中添加多个条目
我必须使用firebase数据库创建订单 我需要在整体操作中执行以下步骤Java 同时在firebase中添加多个条目,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我必须使用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( r
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(
或者通过交易,如果是,如何进行?根据您的评论: entires不是用户的子项,每个都来自不同的列表。用户、订单、交易和商店 我建议您对JSON树中的多个位置同时执行更新,只需调用DatabaseReference的方法 这方面的一个例子是:
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);
}
private void writeNewPost(字符串用户ID、字符串用户名、字符串标题、字符串正文){
//在/user posts/$userid/$postd和
///posts/$posted
String key=mDatabase.child(“posts”).push().getKey();
Post Post=新帖子(用户ID、用户名、标题、正文);
Map postValues=post.toMap();
Map childUpdates=newhashmap();
childUpdates.put(“/posts/”+键,postValues);
childUpdates.put(“/user posts/”+userId+“/”+key,postValues);
mDatabase.updateChildren(childUpdates);
}
本例使用push()
在节点中创建一个帖子,其中包含/posts/$postid
上所有用户的帖子,同时使用getKey()
检索密钥。然后可以使用该键在/user posts/$userid/$postd
的用户帖子中创建第二个条目
以这种方式进行的同步更新是原子性的:要么所有更新都成功,要么所有更新都失败