0Day Forums
Multiple Query and Ref Firebase Database Android - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: Database (https://zeroday.vip/Forum-Database)
+---- Forum: Firebase (https://zeroday.vip/Forum-Firebase)
+---- Thread: Multiple Query and Ref Firebase Database Android (/Thread-Multiple-Query-and-Ref-Firebase-Database-Android)



Multiple Query and Ref Firebase Database Android - vitelline461 - 07-31-2023

My firebase database looks like this:

[![database][1]][1]




From this query

String currentUserID = FirebaseAuth
.getInstace()
.getCurrentUser()
.getUid();

DatabaseReference favorsRef = FirebaseDatabase
.getInstance()
.getReference()
.child("favors");
Query myChatsQuery = favorsRef
.orderByChild("uid")
.equalTo(currentUserID);

I need to create other query to get just the tuples from myChatsQuery who has the child called "messages". If the tuple doesn't have the child "messages", I don't want to insert it in my new query.

So, How I can create a query from other query?

Help me please,

Thanks in advance.


[1]:



RE: Multiple Query and Ref Firebase Database Android - pewing958937 - 07-31-2023

To solve this, please use the following code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("favors").orderByChild("uid").equalTo(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
if(ds.child("messages").exists()) {
//Do what you need to do
}
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
query.addListenerForSingleValueEvent(valueEventListener);

As you can see, you can solve this by querying the database only once.


RE: Multiple Query and Ref Firebase Database Android - gabbib - 07-31-2023

Unfortunately, `orderByChild()` does not work on multiple same fields at once, so you may have to do the job in two steps.

First you can run an `orderByChild()` query looking for the `currentUserId` and then add all those that match to an ArrayList and then run `orderByChild()` in those found `uids` for `messages`.

This looks something like this, in code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-java -->

reference.orderByChild("uid").equalTo(currentUserId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

if(dataSnapshot.exists())
array.add(dataSnapshot.getValue(String.class);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
)};

<!-- end snippet -->

This code above adds `uids` matching `currentUserId` to the ArrayList<String> `array`.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-java -->

databaseReference.child(array.get(0)).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

if(dataSnapshot.child("messages").exists())
// do your thing

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}

)};

<!-- end snippet -->

You can also just use a for loop to go through all the values in the `array`.