简体   繁体   English

如何在Firebase中基于ID更新其他用户余额?

[英]how can i update another user balance base on id in firebase?

i am doing android studio project using Firebase,currently i had done with the registration and login,but my function is to transfer amount to another users with balance. 我正在使用Firebase进行android studio项目,目前我已经完成了注册和登录,但是我的功能是将余额转移到其他用户。

So what i need to do is to find the way to modify/update the balance content(value) after transferring the amount to another user. 因此,我需要做的是在将金额转移给另一位用户之后,找到修改/更新余额内容(值)的方法。

but i do some researched they say Firebase only can only read/write with own user account. 但是我进行了一些研究,他们说Firebase仅可以使用自己的用户帐户进行读写。

assume i have already login to user1,and want to do the transfer to user2. 假设我已经登录到user1,并想转移到user2。

thanks. 谢谢。

-Users
      -AsjdnskkhdiioAndmmnekwas

         Name:"User1"

         email:"user1@hotmail.com"

         uid: "Adjshdkjwwnekwihwoi4kdnw4l2"

         age:20

         Balance:100

      -DdmkenklahaoinskaAnmdmls

         Name:"User2"

         email:"user2@hotmail.com"

         uid: "Cbdnaknekmmalsmdlen1qnio2"

         age:21

         Balance:100
-------------------------------------------------------------------
**how to become:**

 -Users
      -AsjdnskkhdiioAndmmnekwas

         Name:"User1"

         email:"user1@hotmail.com"

         uid: "Adjshdkjwwnekwihwoi4kdnw4l2"

         age:20

         Balance:50

      -DdmkenklahaoinskaAnmdmls

         Name:"User2"

         email:"user2@hotmail.com"

         uid: "Cbdnaknekmmalsmdlen1qnio2"

         age:21

         Balance:150

Remember that you are manager all information of all users (in your database). 请记住,您是管理员(数据库中)所有用户的所有信息。 Use DatabaseReference setValue() method to update information inside database like this: 使用DatabaseReference setValue()方法来更新数据库内部的信息,如下所示:

private DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();

String senderId = "sample_id_sender";
String receiverId = "sample_id_receiver";
mDatabase.child("users").child(senderId ).child("Balance").setValue(new_balance1);
mDatabase.child("users").child(receiverId).child("Balance").setValue(new_balance2);

How to know id of receiver??? 如何知道接收者的ID? it depends on your situation, example, you want to send 1$ to user whose name is "User2". 这取决于您的情况,例如,您要向名称为“ User2”的用户发送1 $。 You must find user2 id in list of users, in you database, I think these guide is good for you: https://firebase.google.com/docs/database/android/read-and-write https://firebase.google.com/docs/database/android/lists-of-data 你必须找到在用户列表user2的ID,在你的数据库,我觉得这些指南是对你有好处: https://firebase.google.com/docs/database/android/read-and-write https://开头火力点。 google.com/docs/database/android/lists-of-data

Interesting problem. 有趣的问题。 I think you can use a combination of a modified data model, a transaction and server-side security rules to implement this. 我认为您可以结合使用修改后的数据模型,事务和服务器端安全规则来实现此目的。

Step-wise: 分步进行:

  1. Since you'll need to know the current balance of both users in order to determine their new balance, you'll need to use a transaction. 由于您需要知道两个用户的当前余额才能确定他们的新余额,因此需要使用交易。
  2. You'll need to store the actual transaction in some way, as otherwise the server won't be able to validate the write. 您将需要以某种方式存储实际的事务,否则服务器将无法验证写入。 The minimum info here is from UID , to UID and amount . 这里的最小信息是from UID to UIDamount
  3. In your security rules you should then modify that the total balance across the two accounts is unmodified across the transaction. 然后,在安全规则中,您应该修改整个交易中两个帐户之间的总余额是未修改的。 For this you need the data from step 2, as you otherwise can't know the to account. 为此,您需要来自第2步的数据,否则您将无法知道to帐户。
  4. Finally, in your security rules you'll want to validate that the only account whose balance can go down is the one of which the current user is the owner. 最后,在安全规则中,您需要验证余额可以减少的唯一帐户是当前用户是所有者的帐户。 In rules something like "balance": { ".validate": "data.child('uid').val() == auth.uid || newData.child('Balance').val() > data.child('Balance').val()" } 在规则中,例如"balance": { ".validate": "data.child('uid').val() == auth.uid || newData.child('Balance').val() > data.child('Balance').val()" }

Scalability may be an issue though, as it's modifying nodes in multiple child nodes which means the transactions run across the entire top-level branch. 但是,可伸缩性可能是一个问题,因为它正在修改多个子节点中的节点,这意味着事务遍及整个顶级分支。

Also see: 另请参阅:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM