How can I use a transaction while performing a multi-location update in Firebase?

后端 未结 2 666
余生分开走
余生分开走 2020-12-06 17:13

In my Firebase database, I need two write to two locations at once. I have rules for both locations that ensure that a user can\'t write there without simultaneously writing

2条回答
  •  眼角桃花
    2020-12-06 17:39

    It is now possible to combine multi-path updates with transactions with the introduction of ServerValue.increment()

    Example:

    Map postLikeUpdate = new HashMap<>();
    postLikeUpdate.put("postLikedBy/" + postId + "/" + userId, true);
    postLikeUpdate.put("likesCount/" + postId, ServerValue.increment(1));
    FirebaseDatabase.getInstance().getReference().updateChildren(postLikeUpdate);
    

    Link to release notes

提交回复
热议问题