How to compare two children nodes from different references in Android Firebase? - java

In Firebase I have the "Users" node where I store information about the registered users and I have the "tasks" node with the tasks information created by each user and 3 owners email addresses added to these tasks manually. I am trying to compare the email field from Users with each owners email. If the current user's email is one of the 3 owner emails, then I will show only the tasks where his email is among those.
This is a snippet from my Firebase realtime database:
"Users" :
{"SolD4tqjUJd1Xru3mRliwtoik2A3" :
{ "email" : "x#gmail.com",
"id" : "SolD4tqjUJd1Xru3mRliwtoik2A3",
"name" : "AB",
"phone" : "123456789"
}
},
"tasks" :
{ "-LgrtyuTjd2QpNIhUeEi" :
{ "-Lgsjx1c-E6OU3t1SbhL" :
{ "id" : "-Lgsjx1c-E6OU3t1SbhL",
"owner_one" : "x#gmail.com",
"owner_three" : "y#gmail.com",
"owner_two" : "z#gmail.com",
"projectId" : "-LgrtyuTjd2QpNIhUeEi",
"taskDate" : "2019 / 6 / 5",
"taskDescription" : "dddd",
"taskName" : "ddddd",
"taskstatus" : "Closed",
"userId" : "SolD4tqjUJd1Xru3mRliwtoik2A3"
}
}
}
}
I have tried the following code to get the email from Users and the owners emails from tasks, but I get stuck at comparing the email with email1, email2 and email3.
myTasks = findViewById(R.id.myTasks);
myTasks.setLayoutManager(new LinearLayoutManager(this));
tasks = new ArrayList<>();
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
UsersRef.child(currentUserID).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
String email = user.getEmail();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
reference = FirebaseDatabase.getInstance().getReference().child("tasks");
reference.addChildEventListener(new ChildEventListener(){
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
//set code to retrieve data and replace layout
for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
Task p;
p = dataSnapshot1.getValue(Task.class);
String email1 = p.getOwner_one();
String email2 = p.getOwner_two();
String email3 = p.getOwner_three();
tasks.add(p);
}
taskAdapter = new TaskAdapter(MyTasks.this, tasks);
myTasks.setAdapter(taskAdapter);
taskAdapter.notifyDataSetChanged();
}

There is no need to create a database call in order to be able to get the email address of the user, you can simply get the email address directly from the FirebaseUser object like this:
String email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
According to your database schema, please note that there is no way you can query your database when you have two nodes with dynamically (pushed) names. There are also no wildcards in Firebase. To solve this, you should reduce the number of children by adding the project's id as a property of your task object. Now, to get all task that corresponde to a specific email address, please use the following query:
reference = FirebaseDatabase.getInstance().getReference().child("tasks");
Query firstQuery = reference.orderByChild("owner_one").equalsTo("x#gmail.com");
This works only with one property. Unfortunately, you cannot query in Firebase realtime database using multiple "WHERE" clauses (in SQL terms). What you can do is to query the database three times, once for every owner property. The second and the third query should look like this:
Query secondQuery = reference.orderByChild("owner_two").equalsTo("x#gmail.com");
Query thirdQuery = reference.orderByChild("owner_three").equalsTo("x#gmail.com");
Please note, that in order to get the desired result, you should use nested queries that might look like this:
ChildEventListener childEventListener = new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
if(dataSnapshot.exists()) {
//Perform the second query and then the third query
}
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {}
};
firstQuery.addChildEventListener(childEventListener);
However, this can be more easily done in Cloud Firestore, where you can add a property of type array and use whereArrayContains() method like this:
tasksRef.whereArrayContains("owners", "x#gmail.com");

Related

Firebase Realtime Database geting User by Email/Name in Android with Java

I want to get a User or just a User UID via email or name. Tried to write the query, but I'm getting all users and then iterating through them and then getting the user but I think it's an expensive task. How can I get only one User/Uid from Realtime Database?
This is what I came up with (But don't think is the best way):
DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("Users");
Query emailQuery = usersRef.orderByChild("email").equalTo(client.getEmail());
emailQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot child : snapshot.getChildren()) {
if (child.getValue(User.class).getEmail().equals(client.getEmail())){
User user = child.getValue(User.class);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
According to your last comment:
I have the corresponding details of each user (email, name), and I want to get the UID of one user (no matter which one)
To get a particular user from the database based on a particular email address, the query that you are already using:
Query emailQuery = usersRef.orderByChild("email").equalTo(client.getEmail());
Returns only the users who have the field email set to what client.getEmail() returns. For instance, if client.getEmail() returns tedo#gmail.com then you'll get a single result, according to your screenshot, which is the first one. That being said, the following if statement, doesn't make sense to be used:
if (child.getValue(User.class).getEmail().equals(client.getEmail())){
User user = child.getValue(User.class);
}
Since the key of the user node is represented by the UID, then you should get it like this:
Query emailQuery = usersRef.orderByChild("email").equalTo(client.getEmail());
emailQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot child : snapshot.getChildren()) {
User user = child.getValue(User.class);
String uid = child.getKey();
//Do what you need to do with UID.
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
Log.d("TAG", error.getMessage()); //Never ignore potential errors!
}
});
That's the simplest, cheapest way to query the Realtime Database, in which you return only the elements that you are interested in and that satisfy a particular condition.

dataSnapShot.getChildren() returning null

I'm trying to fetch the user details of a particular user using a query:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child("user_account_settings"))
.orderByChild("user_id")
.equalTo(getItem(position).getUser_id());
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange: the fetched user id is " + getItem(position).getUser_id());
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
//do stuff
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Firebase database screenshot
The user id that is fetched is correct according to the database and it clearly has children, but for some reason dataSnapshot.getChildren() is empty thus not allowing me to iterate through the foreach loop.
The problem in your code lies in the fact that the user ID property is called in the database userid, while in your query you are using user_id, which is incorrect. The name of the field in your query must match the name field in the database. To solve this, simply change:
.orderByChild("user_id")
To:
.orderByChild("userid")

How to get a child's data when it has been pushed?

I am using the "push()" function to store objects into my firebase database and since it stores data with unique keys I can't change the stored objects values using what's mentionned in the documentation:
myRef.child("child name").setValue(model);
because I simply don't know the key. I wan wondering is there an other way to modify the data of firebase database.
I want to add a new child called "conversations" to the users and programmatically add childs to him .
and this is the databse structure:
If you have the following database:
Users
pushId
name : john
age : 100
In case you dont know the pushId, you can do the following:
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Users");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
String key = ds.getKey();
String name = ds.child("name").getValue().toString();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
The above will get you the name and the key using getKey()

Firebase Android Wrong User displayed

It's showing something, but from the wrong user, not the signed-in user. In fact, it is the latest registred user. Any suggestions? Here is my code:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getInstance().getReference("userInfos");
ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
UserInformation userInformation = dataSnapshot.getValue(UserInformation.class);
vorname.setText(userInformation.vorname);
nachname.setText(userInformation.nachname);
alter.setText(userInformation.alter);
sprachen.setText(userInformation.sprachen);
System.out.println("Prev: " + s);
}
Structure of my database
Well, the type of listener you're using is the ChildAdded one that will ge triggered when you add a new user (that's why you're getting the last added user) To get the current user data user, use a value event listener and pass the logged user id:
String UID = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref = database.getInstance().getReference("userInfos");
ref.child(UID).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
UserInformation userInformation = dataSnapshot.getValue(UserInformation.class);
vorname.setText(userInformation.vorname);
nachname.setText(userInformation.nachname);
alter.setText(userInformation.alter);
sprachen.setText(userInformation.sprachen);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
When you are using the following line of code:
System.out.println("Prev: " + s);
The s variable holds a value of type String that is actually coming from the second argument of the overridden method onChildAdded(). According to the offical documentation, the second argument can be also written as:
onChildAdded(DataSnapshot snapshot, String previousChildName)
That's why you are printing a wrong name. So the second argument is actually the name of the previous child.

How to only get child keys in Firebase

I want to display only children of location i.e Sindh and Punjab (not their children). Is it possible, and if so how can I do it?
From Best practices for data structure in the docs:
Avoid nesting data
Because the Firebase Realtime Database allows nesting data up to 32
levels deep, you might be tempted to think that this should be the
default structure. However, when you fetch data at a location in your
database, you also retrieve all of its child nodes. In addition, when
you grant someone read or write access at a node in your database, you
also grant them access to all data under that node. Therefore, in
practice, it's best to keep your data structure as flat as possible.
That is how Firebase works: If you get an item, you get its children as well. If you don't want this, you should restructure the database.
Avoid Nesting Data
Because the Firebase Realtime Database allows nesting data up to 32 levels deep, you might be tempted to think that this should be the default structure. However, when you fetch data at a location in your database, you also retrieve all of its child nodes. In addition, when you grant someone read or write access at a node in your database, you also grant them access to all data under that node. Therefore, in practice, it's best to keep your data structure as flat as possible.
Below is a structure of yours on how to implement flat database as well as how to retrieve the location key.
{
"location": {
"punjab": {
"lahore": true,
"multan": true
},
"sindh": {
"karachi": true
}
},
"place": {
"lahore": {
"location": "punjab",
"details": {
"0": "model",
"1": "steel town"
}
},
"multan": {
"location": "punjab",
"contacts": {
"0": "myarea"
}
},
"karachi": {
"location": "sindh",
"contacts": {
"0": "hadeed",
"1": "Steel town"
}
}
}
}
Below is the code that you can use to retrieve the location key.
private DatabaseReference mDatabase;
// ...
mDatabase = FirebaseDatabase.getInstance().getReference();
mLocation = mDatabase.child("location");
mPlace = mDatabase.child("place");
ValueEventListener placeListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
Place place = dataSnapshot.getValue(Place.class);
String location = place.location;
System.out.println(location);
}
};
mPlace.addValueEventListener(placeListener);
For more information on Firebase:
Firebase Security & Rules
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("location");
final ArrayList<String> statesArrayList= new ArrayList<>();
databaseReference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
statesArrayList.add(dataSnapshot.getKey());
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Yes, you can
ValueEventListener getValueListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get data from firebase
Log.d("The name of child that you need:", dataSnapshot.getKey());
// ...
}
};
databaseReference.addValueEventListener(getValueListener );
Read more: https://firebase.google.com/docs/database/android/read-and-write#listen_for_value_events
Though nesting data is not recommended in NoSQL databases like Firebase, to get the name of child nodes your code will look like this
DatabaseReference mainRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference locationref = mainRef.child("location");
final ArrayList<String> locationNames = new ArrayList<>();
locationref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
locationNames.add(dataSnapshot.getKey());
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
You can now use locationNames to do what you want.
Firebase can as quickly look up a node at level 1 as it can at level 32. Depth is not a factor that affects speed on a technical level but years of experience with Firebase has shown that deeply nested data often goes hand in hand with performance problems. As an example, i recomand you reading the offical documentation, Best practices for data structure in Firebase and Structuring your Firebase Data correctly for a Complex App.
If you don't want to change your actual database structure and assuming that location node is a direct child of your Firebase root, please see the code below:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference locationRef = rootRef.child("location");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
Log.d("TAG", key);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
locationRef.addListenerForSingleValueEvent(eventListener);
Your output will be:
punjab
sindh
I know this question was asked a while ago and for android, but I was looking for a solution to this and came upon this question. I couldn't use the answers provided, so here is the solution I found. This will only get the children and nothing else.
This is in javascript, but you can use whichever method is the alternative in java to make a request from a REST endpoint.
const databaseURL = "https://example.firebaseio.com" // replace value with yours
const endpoint = "location"
const user = firebase.auth().currentUser
user.getIdToken()
.then(token => {
return fetch(`${databaseURL}/${endpoint}.json?auth=${token}&shallow=true`)
})
.then(response => {
return response.json()
})
.then(json => {
console.log({json}) // contains only the children
})
.catch(error => {
console.log(error.message)
})
The important bit here is the &shallow=true otherwise you get all the data in the children.
Using curl this would be
curl 'https://example.firebaseio.com/location.json?auth=INSERT_TOKEN&shallow=true'
This assumes location is a child of the root.
More details can be had by looking at the docs
Before calling this, you have to make sure you have the currentUser available in firebase.
Also, I would heed the warnings mentioned here about proper firebase realtime database structure. However, if you are already in this spot, then this can help.
This is how u can get child names
HashMap<String, Object> allData = (HashMap<String, Object>) dataSnapshot.getValue();
String[] yourChildArray = allData.keySet().toArray(new String[0]);

Categories