[英]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: 分步进行:
from UID
, to UID
and amount
. from UID
to UID
和amount
。 to
account. to
帐户。 "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.