Reputation: 11
I am trying to query the database to check through multiple unrelated keys to check a parameter and compile a list that fits my conditions.
I already have a list which was created with a previous query to another node of the database. Which returned a list of keys which i want to check.
Now i want to iterated through this list and query them to see if they fit my conditions.
I have tried downloading whole node and checking client side but this would become very slow if the node grows and download a lot of unnecessary data.
I have tried putting a loop around my query and sending multiple queries within, on the keys in my list. This worked but becomes increasingly complicated as i try to go back to the beginning with different conditions if my original conditions were not met.
for (int i = 0; i < nextUp.size(); i++) {
Query query = myRef.child(nextUp.get(i).toString());
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
boolean v = false;
for (DataSnapshot likesSnapshot : dataSnapshot.getChildren()) {
if (uid.equals(likesSnapshot.getKey())) {
v = true;
} else {
}
//b if for second pass with different conditions
if (b) {
if (!v) {
complieList(dataSnapshot.getKey(), b);
}
if (!b) {
complieList(dataSnapshot.getKey(), b);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
By the time my code goes back to the beginning all the queries have not yet complete. So i need to wait for all to finish to continue. But as this requires waiting for the results for many queries i cannot simply put it in the onDataChange. I have seen that a JavaScript promise can send multiple queries at once in a pipeline and receive the result for all of them. Is there a way to do this in Java.
Update
Here is a link to what i want to do but it is for javascript Querying By Multiple Keys in Firebase
Upvotes: 1
Views: 734
Reputation: 4621
Did you consider using transaction?
Transaction is used to composite serveral db request and request one time.
It is also used to prevent data change during db request.
by the way, realtime db doesn’t support complicate query
you can use only orderbychild.. etc
But you can store data separately when data is saved.
Upvotes: 1