代码之家  ›  专栏  ›  技术社区  ›  Muhammad Umar

同时在firebase中添加多个条目

  •  1
  • Muhammad Umar  · 技术社区  · 7 年前

    我必须使用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(
    

    1 回复  |  直到 7 年前
        1
  •  2
  •   Alex Mamo    7 年前

    实体不是用户的子级,每个实体来自不同的列表。用户、订单、交易和商店

    我建议您对JSON树中的多个位置同时执行更新,只需调用DatabaseReference的 updateChildren()

    一个例子来自 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);
    }
    

    push() 在节点中创建包含所有用户的帖子的帖子 /posts/$postid getKey() . 然后可以使用该键在用户的帖子中创建第二个条目 /user-posts/$userid/$postid .