1.DatabaseReference db=FirebaseDatabase.getInstance().getReference("users");
1.Query query = db.orderByKey().equalTo(uid);
2.FirebaseDatabase.getInstance().getReference("users").orderByChild("token").equalTo(toKen)
Does any one of the above two codes written in Java Android Studio send the entire "users" node of Realtime Database to the client Android App or Only the the Fetched Records will be send to the client Android App. It Seems like that it sends the complete "users" node to the client Android App, because my realtime database bill is quite high and customers are low.
It seems you are looking for orderByChild(). To get the child node where the value of uid is equal to passed UID, try this:
DatabaseReference db=FirebaseDatabase.getInstance().getReference("users");
Query query = db.orderByChild("uid").equalTo(uid);
The documentation says,
Method
Usage
orderByChild
Order results by the value of a specified child key or nested child path.
orderByKey
Order results by child keys.
The second query looks fine and should fetch the node where the value of token is equal to the supplied token.
If the download size is higher than you'd expect based on the number of query results, check if you've defined an index on the token property. If no such index exists, Firebase will download all data under users to the client, and perform the sort/filter there. If an index is declared, the ordering/filtering is done on the server.
Related
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.
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.
assume i have already login to user1,and want to do the transfer to 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:
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??? it depends on your situation, example, you want to send 1$ to user whose name is "User2". 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
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:
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.
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.
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.
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()" }
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:
Is the way the Firebase database quickstart handles counts secure?
i don't get way to view images according my category my database will be like this
-id
-category : A
-image : URL
I WANT TO SHOW A category in A category activity
You'll need to run a query for that. Firebase has two databases these days and the code you need depends on which one you use.
If you're using the Firebase Realtime Database, the query is:
Query query = FirebaseDatabase.getInstance().getReference("images")
.orderByChild("category").equalTo("A")
After that you need to read the results of this query, as shown in this documentation.
If you're using Cloud Firestore, the query is:
Query query = FirebaseFirestore.getInstance().getCollection("images)
.whereEqualTo("state", "CA");
And then you need to read the results of this query as shown in this documentation.
i am trying to get username and email for single user profile.
I think that firebaseAuth.getCurrentUser().getUid() return uid that is not the same as uid in FirebaseDatabase. You can user Query.
For example:
Query q = firebaseDatabase.child("users").orderByChild("email").equalTo(firebaseAuth.getCurrentUser().getEmail());
You should use addChildEventListener instead of addValueEventListener, and write the code inside onChildAdded.
You can find the explanation from documentation:
When working with lists, your application should listen for child
events rather than the value events used for single objects.
I am using this query in my android app :
fireStoreQuestions.document(subject).collection(subject)
.whereEqualTo("reviewed", true)
.whereGreaterThan(FieldPath.documentId(), randomId)
.limit(1)
.get()
I want to know how to add an index to this query in cloud firestore. I don't know how to represent the document id in the index.
You can create the index manually in your Firebase Console or if you are using Android Studio, you'll find in your logcat a message that sounds like this:
W/Firestore: (0.6.6-dev) [Firestore]: Listen for Query(subject where reviewed == true and id > randomId) failed: Status{code=FAILED_PRECONDITION, description=The query requires an index. You can create it here: https://console.firebase.google.com/project/...
So you can simply click on that link or copy and paste the url into a web broswer and you index will be created automatically.
I'm trying to display certain data in my app using a graph.
The data is saved in Firebase database under multiple nodes, like the highlighted data in this database.
Is there any library that can be used to make real-time graphs from Firebase?
You might need to restructure your data, instead of storing the workout data in the user object, try breaking it out into it's own object.
/workouts {
/{refPush} {
date: '19-2-2018'
name: 'Test Graph 2',
set: '20',
uid: 'someUid',
}
}
Then you could access this information like;
firebase.database().ref('/workouts')
.orderByChild('set')
.equalTo('20');