【问题标题】:Is it possbile to add/update two different children nodes on android firebase是否可以在 android firebase 上添加/更新两个不同的子节点
【发布时间】:2018-08-08 03:52:59
【问题描述】:

在我的 firebase 数据库上

是否可以在子 Transactions 上添加月份、姓名、付款,同时更新子 Users 的 DATE 数据?

如果有怎么办?如果不是,还有哪些替代方法,以便当我在交易中添加新孩子时,我可以在这样做时更新我的​​用户的日期?

【问题讨论】:

  • 是的,您可以使用multi-location updates
  • 有没有其他不使用hashMap在多个节点更新的方法?

标签: android firebase firebase-realtime-database


【解决方案1】:

是的,你可以。 Firebase 具有一项功能,允许您通过一次调用同时更新 JSON 树中的多个位置。以这种方式进行的同时更新是atomic:要么所有更新成功,要么所有更新失败。

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/Accounts/Transactions/" + transactionKey, transactionMap);
childUpdates.put("/Users/" + userId + "/", userMap);
rootRef.updateChildren(childUpdates);

更多信息here.

【讨论】:

  • 能否请教一些想法或方法,以便获取我想要更新数据的用户的用户 ID?前任。我可能可以获得每个事务的唯一密钥,但是在获取要更新的用户的唯一密钥或 userId 时我迷失了
  • 因为我看到你正在使用身份验证,所以要获取 userId 请使用以下代码行:String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
  • 嗯,如果我必须获取我未登录的用户的用户 ID 怎么办?
  • 在这种情况下,您需要根据另一个属性而不是uid 来查找该用户,比如说电子邮件地址,获取userId,进行更新,对吗?跨度>
  • 哦对了!!! aaa 所以嗯,我怎样才能获得每个用户的唯一电子邮件地址?嗯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
相关资源
最近更新 更多