Rüçhan Okal
Rüçhan Okal

Reputation: 23

How can I filter and sort my documents in Firestore RecylerView (Firestore UI)?

I want to sort and filter my documents like the below code. But it didn't work. Actually, when I add a new document I can't see it in real-time.

val query: Query = FirebaseFirestore.getInstance()
            .collection("Lessons")
            .whereEqualTo("tag",tag)
            .orderBy("order",Query.Direction.ASCENDING)


        val options: FirestoreRecyclerOptions<Model> = FirestoreRecyclerOptions.Builder<Model>()
            .setQuery(query, Model::class.java)
            .build()

        adapter = KonuRecyclerAdapter(options,tag,konuBasligi)
        binding?.konularAnaRecyclerView?.adapter = adapter

But when I comment "whereEqualto" line I can see new documents (Newly added). After that, if I uncomment this line again, I can see this document too. I want that when I add a new document it will show in real-time. Please help me with this problem. You can warn me of any mistakes that I did. Any help will be appreciated!!!

Upvotes: 2

Views: 214

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 139019

As far as I can tell you are trying to use a Query that contains a call to:

.whereEqualTo("tag",tag)

And

.orderBy("order",Query.Direction.ASCENDING)

On a different field. This kind of query requires an index. To see how you can solve this, please see my answer from the following post:

However, using a call to .whereEqualTo() without an ordering or, a call to .orderBy() without a .whereEqualTo() call, doesn't require an index. That's the reason why it's working when you comment one, or the other method calls.

Upvotes: 1

Related Questions