I know this has been asked 100 times already but none of the solutions seem to be working for me.
Want to read the database of "user_preferences" for the user that is signed in (userID) and read the gender/age/weight/height values and store them in the variables shown below. currently returns null on everything (the log statement and the values). Feel like i havent got the path set up properly or something. help would be great!
and my code
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
userID = mAuth.getCurrentUser().getUid();
DatabaseReference testRef = myRef.child("user_preferences");
testRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) { //loop through the firebase nodes
UserPreferences userPreferences = new UserPreferences();
userPreferences.setAge(ds.child(userID).getValue(UserPreferences.class).getAge());
userPreferences.setHeight(ds.child(userID).getValue(UserPreferences.class).getHeight());
userPreferences.setWeight(ds.child(userID).getValue(UserPreferences.class).getWeight());
userPreferences.setGender(ds.child(userID).getValue(UserPreferences.class).getGender());
genderSet = userPreferences.getGender();
age = userPreferences.getAge();
height = userPreferences.getHeight();
weight = userPreferences.getWeight();
Log.d(TAG, "onDataChange: " + genderSet);
//
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
These two lines of code:
DatabaseReference testRef = myRef.child("user_preferences");
testRef.addValueEventListener(...)
are effectively querying the entire node called user_preferences. That means everything at that location - all users. It sounds like this is not what you want. If you want to query just a single user, you should be specific about that in your query by adding the userID that you want to the query location:
DatabaseReference testRef = myRef.child("user_preferences").child(userID);
testRef.addValueEventListener(...)
Also, these lines of code are confusing to me:
userPreferences.setAge(ds.child(userID).getValue(UserPreferences.class).getAge());
userPreferences.setHeight(ds.child(userID).getValue(UserPreferences.class).getHeight());
userPreferences.setWeight(ds.child(userID).getValue(UserPreferences.class).getWeight());
userPreferences.setGender(ds.child(userID).getValue(UserPreferences.class).getGender());
You're deserializing a UserPreferences object for each and every field you want to populate, which is wasteful. It seems to me that you really just want to deserialize it once and remember the object:
UserPreferences userPreferences = dataSnapshot.getValue(UserPreferences.class);
Regarding the null values, you seem to be using external fields, which will not be set until the Firebase returns the network call after at least a second. Your values will be null in the meantime, so you should not be setting them onto a UI element outside of onDataChange.
Also, you have a lot of gets/sets going on, when you only need to call one getValue() for the class, then additional ones for the fields.
Then, you don't seem to want to loop over anything, so you should directly access the user node from the top reference.
For example,
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
userID = mAuth.getCurrentUser().getUid();
DatabaseReference testRef = myRef.child("user_preferences/"+userID);
// or .child("user_preferences").child(userID)
testRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
UserPreferences userPreferences = dataSnapshot.getValue(UserPreferences.class);
Log.d(TAG, "onDataChange: " + userPreferences.getGender());
// TODO: Update some UI element here
}
#Override
public void onCancelled(DatabaseError databaseError) {
// TODO: Add error handling
}
});
}
If you only want to read the values once, use testRef.addListenerForSingleValueEvent()
Related
Below is the code that I am using to save value into firebase. It generates a random key for every value. (I want to save redundant data, so this method was necessary).
GenderOfNameHelperClass GONHC = new GenderOfNameHelperClass();
reference = FirebaseDatabase.getInstance().getReference().child("GenderOfName");
GONHC.setName(Name);
GONHC.setGender(Gender);
reference.push().setValue(GONHC);
This creates a structure as below:
db-ai-34201
-...GenderOfName
-...M4oLyB1ADGLWGJG4EgR
...gender:"Female"
...name: "akira"
Now how can I fetch the data from this.
I want to fetch the gender, when name is searched from my android UI.
This is the listener to the GenderOfName node to extract data under the random keys:
ValueEventListener listener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapShot:dataSnapshot.getChildren() ){
//these are the values of gender and name for every random key
//in GenderOfName node
String gender = snapShot.child("gender").getValue(String.class);
String name = snapShot.child("name").getValue(String.class);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // never ignore errors
}
};
Finally attach the listener to your reference of that node to start listening to it
reference = FirebaseDatabase.getInstance().getReference().child("GenderOfName");
reference.addValueEventListener(listener);
UPDATE
you can always use a query to filter results:
instead of this:
reference = FirebaseDatabase.getInstance().getReference().child("GenderOfName");
reference.addValueEventListener(listener);
try this,it will give you a specific snapshot based on the name you need:
Query query = FirebaseDatabase.getInstance().getReference().child("GenderOfName").orderByChild("name").equalTo(theNameYouWant);
query.addValueEventListener(listener);
I get the Alex Mamo's Solution in order to check whether a unique value exists in my database but the snapshot gets always null. The solution is here:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userNameRef = rootRef.child("Users").child("Nick123");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.exists()) {
//create new user
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
userNameRef.addListenerForSingleValueEvent(eventListener);
And my database looks like this:
users
(auto-generated push token ID)
fullname
username
gender
...
I don't know whether it's the right method but I used push() method for adding the user object to the database and this method generates push token ID like above.
So, in order not to create duplicate users, what should I change with the above solution? Is there a better method to do this? Maybe like checking the firebase uid with some firebase auth methods?
UPDATE
My database screenshot:
As you see the string just below 'users' is not the same as user's uid.
This is not the best practice when it comes to save user details into the database. First, you should implement Firebase Authentication and then in order to check if a user exist in your database, you should use the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("users").child(uid);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.exists()) {
//create new user
}
}
#Override
public void onCancelled(DatabaseError error) {
Log.d("TAG",error.getMessage()); //Don't ignore potential errors!
}
};
uidRef.addListenerForSingleValueEvent(eventListener);
So the key for solving this problem is to call the child() method and pass the UID as an argument instead of calling the push() method that generates an random unique identifier.
If you have this database:
users
(auto-generated push token ID)
fullname
username
gender
Then to check if user exists you need to do the following:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userNameRef = rootRef.child("users");
Query queries=userNameRef.orderByChild("fullname").equalTo("Nick123");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.exists()) {
//create new user
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
queries.addListenerForSingleValueEvent(eventListener);
First if you have users lowercase letter, then inside child add users, then you need to use a query to be able to check if the fullname is equal to Nick123.
Usually the database is structured like this:
users
userId
fullname: Nick123
The attributes are key:value, the key is the identifier and Nick123 is the value, so inside the child() method you need to write the identifier which is fullname and not Nick123.
Then since you are able to get the userId, you can do this:
FirebaseUser currentFirebaseUser =FirebaseAuth.getInstance().getCurrentUser();
String userId=currentFirebaseUser.getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userNameRef = rootRef.child("users").child(uid).child("fullname");
userNameRef.addValueEventListener(new ValueEventListener(){...}
Let's say I have a Firebase Realtime database structure where there are user nodes and each user node has post nodes that only contain post ids. Then there are also separate post nodes where the actual posts reside (flat data structuring). The structure is described in detail in this answer.
Now if I can get the ids of the posts of each user by attaching a listener to that user's posts node, how can I retrieve the posts themselves (full information from a post node)? I know there aren't any queries where one can pass a bunch of keys and get the associated records. Should I just attach a listener to each post node I am interested in? I'm currently afraid there might be some serious performance issues, because the number of posts is practically unlimited.
Using the exact database structure from this post, to solve your problem, you need to query your database twice. Once to get the post ids of the particular user you need and second to get the posts it self. To achieve this, please use the following code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference postsRef = rootRef.child("users").child(uid).child("posts");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String postId = ds.getKey();
DatabaseReference postIdRef = rootRef.child("posts").child(postId);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
String postName = dSnapshot.child("postName").getValue(String.class);
Log.d("TAG", postName);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
postIdRef.addListenerForSingleValueEvent(eventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
postsRef.addListenerForSingleValueEvent(valueEventListener);
I'm working on my app that has a fragment, this fragment displays new users with a follow button using this query :
query = mDatabaseref.child("users").orderByChild("date").limitToLast(10);
After a follow button is clicked, the current User Uid gets saved as a child in the other child that has a name "Follows" with 2 other child's "following" and "followers" as it showed in the below picture :
what I want to do is to make a Query for an other fragment with the name Following and show all the users that the Current user is following with the ability of getting their names, I was thinking of making a data dataSnapshot in the FirebaseRecyclerAdapter onBindViewHolder method basing on the Uid tooken from the childs, but I think it's gonna be complex, on the other hand, i tried to sort the follows in the user Info child direclty, but it leads to a strange behaviour on the RecyclerView.
I'm not going to write you all the code including your FirebaseRecyclerAdapter, I will show you just how to query to get the desired results. As i understand, here is the problem. So in order to achieve this, you need to query your database twice, once to get all those ids and second, based on those ids to get the names. So, please use the following code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference followingRef = rootRef.child("follows").child(uid).child("following");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String userId = ds.getKey();
DatabaseReference userIdRef = rootRef.child("users").child(userId);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
String name = dSnapshot.child("name").getValue(String.class);
Log.d("TAG", name);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
userIdRef.addListenerForSingleValueEvent(eventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
followingRef.addListenerForSingleValueEvent(valueEventListener);
The result in your logcat will be all the names of the followers that belong to a single user.
In my app, I need to check if a given element of my database on firebase has a child with a given name. I hoped it could be done by using something along the lines of:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
if (rootRef.childExists("name")) {
//run some code
}
I searched but I couldn't find anything useful.
Edit 2; worth putting on top: I think it is worth mentioning that this is actually downloading all data at this snapshot just to check whether any data exists. You should be mindful here. If the reference is huge (e.g. actually the root reference and not a specific child/property) then you should either find a deeper node you can use to check for existence or design your data structure differently so an efficient check is possible.
A database reference is effectively the URL for that data. You want to actually get data to see whether a child exists. This is why the method you seem to be looking for is on DataSnapshot.
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
void onDataChange(DataSnapshot snapshot) {
if (snapshot.hasChild("name")) {
// run some code
}
}
});
Now, this design pattern feels a bit strange. You're reading the whole database just to see whether "name" exists. You can make this a bit more efficient by listening to rootRef.child("name") and then just checking whether snapshot.exists().
If you're trying to do validation here, and not control flow, you should consider putting this code in your rules.json.
edit: I originally used the wrong function name (childExists instead of hasChild)
Don't do like this
NEVER
It will take all your data and bring to device
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
void onDataChange(DataSnapshot snapshot) {
if (snapshot.hasChild("name")) {
// run some code
}
}
});
Check it by this way.
It will return the value of the child if exists, otherwise -> null
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("childName")
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
void onDataChange(DataSnapshot snapshot) {
if (snapshot.getValue() == null) {
// The child doesn't exist
}
}
});
A complete solution. No need to download all the data. Just check if the child exist like this:
// Assuming the names are in the name node. Remove "name/" if not
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("name/" + name);
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()) {
// Exist! Do whatever.
} else {
// Don't exist! Do something.
}
}
#Override
public void onCancelled(DatabaseError error) {
// Failed, how to handle?
}
});
Try using .childexists in combination with .equalTo("Your specific name")
UsersRef = FirebaseDatabase.getInstance().getReference();
Users.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.hasChild("childName")) {
// it exists!
}else{
// does not exist
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Use snapshot.exists() to check if the referenced database entry contains a child , irrespective of the value of the child.