Android firebase query equivalence - android

In my Android app I need to compare equivalence of two Firebase database query objects. Is there some way for me to assert that two query objects are equivalent based on their database references and query parameters?
The Javascript Firebase version of the Query class has an isEqual() method which produces the desired behaviour.
https://firebase.google.com/docs/reference/js/firebase.database.Query
I tried the following equivalence test in my Android code but this does not produce expected behaviour:
Reference ref = Firebase.getInstance();
String child = "childKey"; //this is a valid child of ref
Query query1 = ref.child(child).limitTo(10);
Query query2 = ref.child(child).limitTo(10);
query1 == query2 //false
query1.equals(query2) //false

This seems to work
query1.zzWM().toString.equals(query2.zzWM().toString) // true
I don't think you'd need this though. For as far as I know Firebase keeps the results cached for as long as there is a listener on a reference. So even if you would have 2 it would just get it from the cached version, but I might be wrong.

Related

Firebase Firestore OR query (Android Studio)

I've been trying to find a query for almost 2 days now
I want to search id (current user id) from the document 4 fields (customer1,customer2,customer3,customer4)
Here is the firestore document picture
tried this query
final Query userQuery = collectionReference
.whereEqualTo("customer1",firebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("customer2",firebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("customer3",firebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("customer4",firebaseAuth.getInstance().getCurrentUser().getUid());
but this only shows up if the current ID is present in all 4. Is there any easier way to do this.
You can do that by using a field that is an array containing the uids you want to test, and then applying array-contains on it. In your case:
In your case:
customer: [customer1, customer2, customer3, customer4]
collectionReference
.where("customer ", "array-contains", firebaseAuth.getInstance().getCurrentUser().getUid())
Firestore does not support logical OR queries among mulitple fields. So, what you're trying to do is not possible with a single query using the database structure you have now. You would have to perform multiple queries and merge the results in the client.
If you want to be able to use a single query, you will have to change your database. One option is to put all the customers into a single array field and use an array-contains query to find a customer in that field.

Kotlin Firestore Queries Issue

Im using FirestorePagingAdapter in the FireStoreUI framework to create a simple query and get results into a recycler view.
I am able to fetch results and display them if I use a simple query:
var mQuery = FirebaseFirestore.getInstance().collection("test")
But in my application I have a searchView which I will be using to add queries options in mQuery eg:
var mQuery : Query = FirebaseFirestore.getInstance().collection("test")
if (!searchString.isNullOrEmpty()) {
println("SearchView: $searchString")
mQuery.whereGreaterThanOrEqualTo("name", searchString)
}
But this does not work.
Can we not add options to Query after it has been assigned?
Query objects are immutable. They can't be changed after created. Notice from the API documentation that whereGreaterThanOrEqualTo() returns a new Query object (as well as all filters) with the new condition added to it. So, you can just reassign mQuery with the new Query it built.
See also: Conditional where clause in firestore queries

Simple Android / Firebase orderByChild / equalTo

This I suspect is easy, but I can't get it to work as I want. I'm referencing a database ref with my query information for my Firebase database. The code below works fine, but I can't hard code in Match_01 (this was purely done to get the code working).
String getArgument = getArguments().getString("matchid");
final DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Matches").child("Match_01");
What I need to do is use the matchID thats been passed to the fragment and use equalTo instead of referencing the final child node.
final DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Matches").orderByChild("gameID").equalTo(getArgument);
But this doesn't work, I can't swap out the last child reference for the orderByChild reference.
All help is appreciated.
I'm assuming from your question that the Matches node's children have the id you want to reference dynamically as a key.
Then, you need orderByKey instead of orderByChild. This should work:
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Matches");
Query q=ref.orderByKey().equalTo(getArgument);
When using orderByChild, the query will match nodes against this node's attributes. In other words, your attempt would work on a collection with the following structure:
- Matches
- $key
- gameID: "Match_01"

Not know exception with firebase filters

This code:
var query: Query = store.collection(COLLECTION_USERS)
query = query.whereGreaterThanOrEqualTo("age", filter.startAge).whereLessThanOrEqualTo("age", filter.endAge)
query = query.whereGreaterThanOrEqualTo("start_date", filter.startDate).whereLessThanOrEqualTo("end_date", filter.endDate)
query = query.whereEqualTo("sex", filter.sex)
return query.get()
throw exception: java.lang.IllegalArgumentException: All where filters other than whereEqualTo() must be on the same field. But you have filters on 'age' and 'start_date' how to fix?
Official documentation of Firestore says that range filters on different fields are forbidden. So Firestore allows to chain multiple where() methods to create more specific queries but only on the same field.
To achieve what you want, you need to query your database twice, once for each fiter because you cannot use both methods in the same query.
Another way to make it happen would be to store a special flag that might fit the query, although in real world applications it will almost impossible to store every single way a user might query the data.

Having trouble creating a Firebase Query requesting only objects with a field of a certain value (in Android)

I have Firebase data that looks like this.
I want to create a Query object in java that retrieves only Invites (those are the objects with randomly generated key's KHcYy...) which have a name = "John Smith".
Here is the code for my current attempt, which is only giving me an empty query:
firebase = new Firebase(ListApplication.FIREBASE_URL);
Firebase objectRef= firebase.child("ExampleParty");
Query q = objectRef.child("brother").equalTo("John Smith");
I tried taking a look through this tutorial on queries, but that only really had info on doing sorty and ranking based queries. I couldn't find any info on this type of task, and the API Reference didn't really make it clear what I'm supposed to be inputting in any of these calls.
Thanks for the help!
Have you tried this...
firebase = new Firebase(ListApplication.FIREBASE_URL);
Firebase objectRef= firebase.child("ExampleParty");
Query q = objectRef.orderByChild("brother").equalTo("John Smith");

Categories

Resources