Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 743 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple Query and Ref Firebase Database Android

#1
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]:
Reply

#2
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.
Reply

#3
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`.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through