Kotlin Firestore Queries Issue - android

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

Related

Is there any way to use OR query with Firestore RecyclerAdapter?

I have a recyclerview using Firestore Recyclerview Adapter, Now I want to show the list by using OR query.
For example I want to show the list which are either in "Pending" state OR "Assigned" state using query. I know firestore doesn't have OR query inbuilt but I want to achieve this anyhow. Any alrernative solution.
I have stored 4 state in firestore db : Pending, Assigned, Accepted and Completed.
Only want to show pending or assigned
Below is my code:
Query query = requestVehiclesCollectionReference
.whereEqualTo("clientId", id)
// show list which are in Pending state or Assigned state
.whereEqualTo("status", "Pending")
.orderBy("createdAt", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Item> options = new
FirestoreRecyclerOptions.Builder<Item>()
.setQuery(query, Item.class)
.build();
Firestore doesn't support queries with OR conditions. See:
Implementing OR in firestore query - Firebase firestore
How to perform compound queries with logical OR in Cloud Firestore?
Firebase Firestore - OR query
The common workaround is to use multiple queries (one for each condition) and merge the results in your application code.
FirebaseUI's adapters show data from a single query or collection. They have no option to show the results from multiple queries/collections.
From this combination it follows that you can't show data from an OR condition with a single FirebaseUI adapter. You will have to create you own adapter for this, although you can certainly use the (open-source) FirebaseUI adapters for inspiration.

Applying Word Stemming in SearchView for fetch data from Firebase database

I need to fetch list of users from Firebase database using SeachView or search dialog and I think word stemming will be best for my app.
Not asking for code but please tell me the alorigthm for it.
To achieve what you want, you need to execute a query which should look like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
Query query = usersRef.orderByChild("name").equalTo(newText);
So everytime you create a search you should return a new query. So according to this, every time you want to filter on a new condition, you will need to:
Create a new query based on the new filter:
Query query = usersRef.orderByChild("name").equalTo(newText);
Attach a listener to this new created query.
Create a new adapter with the results of this new created query, or update the existing one using notifydatasetchanged() method.

Android firebase query equivalence

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.

Sorting Realm Results in Realm Base Adapter?

In my android app, I am using the realmBaseAdapter to dynamically display data from a realm query. I was wondering if there is any way to sort these results within the adapter so that I can group them based on certain properties.
Yes, you can :)
You can you build your RealmResults like this:
RealmResults<User> results = realm.where(User.class).findAllSorted(
"name", RealmResults.SORT_ORDER_ASCENDING);
or sort the it after build it like:
RealmResults<User> result = realm.where(User.class).findAll();
result.sort("age"); // Sort ascending
result.sort("age", RealmResults.SORT_ORDER_DESCENDING);
You can also sort by multi fields like:
RealmResults<AllTypes> results = testRealm.where(AllTypes.class)
.findAllSorted(new String[]{"name", "age"},
new boolean[]{RealmResults.SORT_ORDER_ASCENDING,
RealmResults.SORT_ORDER_ASCENDING});
See Doc of RealmQuery and RealmResults.
You don't have to sort the result every time before using it. Once the results is sorted, it will be sorted even when the database changed and results get updated.

GreenDAO QueryBuilder dynamically add conditions

I currently have a list of userIds and I am trying to create a query to get all of those from my DB.
this is what I have in mind, I'm just not that sure that it's possible:
ArrayList<Users> listOfUsers = getCurrentUsers();
// lets assume that by now I have a list of users
QueryBuilder<Users> qb = getUsersDao().queryBuilder();
for(Users usr : listOfUsers) {
qb.where(Properties.userId.eq(usr.getUserId());
}
List result = qb.list();
I haven't seen any documentation about what is the right way of doing this and I want to know if this is the correct way of creating a dynamic query in GreenDAO.
EDIT:
I tried this and the result was a NullPointerException in the line of the declaration on the QueryBuilder
try using the IN query instead, it will run faster + you can cache your Query object.
so lets say you have
List<String> userIds;
you can get the list with:
qb.where(Properties.UserId.in(userIds))
if this is an operation that you do frequently, it is better to cache the Query. to do that, prepare the query as follows for only once:
Query<User> query = qb.where(Properties.UserId.in("?")).build();
then when you need to run it :
query.setParameter(0, userIds);
return query.list();

Categories

Resources