简体   繁体   中英

How make complex query with FirestoreRecyclerAdapter?

I'm developing an Android app and I need to make a complex query on my Firestore database and create a FirestoreRecyclerAdapter . To create the adpater I need a FirestoreRecyclerOptions object that take in input the whole query. Reading the documentation, I can't use in my query the methods whereGreaterThan , whereLessThan , oderBy , etc, on different parameters. For example, how can I get users from db who have age greater than/less than AND who have weight greater than/less than?

For example the document's structure in my firestore database is:

Users --->UserID--->userName(String)
            --->userAge(number)
            --->userHeight(number)
            --->userWeight(number)

FirebaseFirestore db;

db = FirebaseFirestore.getInstance();

RecyclerView recyclerView;

recyclerView = (RecyclerView)findViewById(R.id.recyclerViewID);
.
.
.

Query query = db.collection("Users").//the complex query that i need

FirestoreRecyclerOptions<User> options = new FirestoreRecyclerOptions.Builder<User>()
            .setQuery(query, User.class)
            .build();

adapter = new UsersAdapter(options, this);//get in input options and the context

recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);

Edit: A possible solution in my last comment to answer 1

There are some query limitations when it comes to Firestore:

Query limitations

Cloud Firestore does not support the following types of queries:

  • Queries with range filters on different fields, as described in the previous section.

So you cannot query your database on range filters using whereGreaterThan() and whereLessThan() methods on different properties and pass it to an adapter.

Edit:

A possible solution for this issue would be to filter your records client side twice. First query the database using the first property and second using the second property. Unfortunately you cannot achieve this in a single go.

Edit2:

The solution would be to query the database using the first query, get the corresponding elements, query the database again using the second query and get the corresponding elements and then merge the results client side. Now the elements from the database were filtered twice. Pass a list of that elements to an adapter and that's it. Note, when using this solution you cannot use the Firebase-UI library anymore but this how you can get your items filtered twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM