0

My app shows a Firebase db through a FirestoreRecyclerAdapter. I have put the code relating to the query in a separate method:

private void queryActive(){

    queryActive = collectionRef
            .whereEqualTo("activeStatus",1);                

}

Each item in the recycler display has a switch through which the status of the underlying item's int field "activeStatus" can be toggled between "active" (1) and "inactive" (0).

The app shows indeed only the items that are "active". If I toggle the switch of a certain item to "inactive", that item indeed disappears. Excellent.

Now I want to order the active items according to their int field "categoryNumber". So I add an orderBy() method.

private void queryActive(){

        queryActive = collectionRef
                .whereEqualTo("activeStatus",1)
                .orderBy("categoryNumber", Query.Direction.ASCENDING);

}

I understand that, as Firebase automatically adds a simple index for every field, there is no need to manually add an index. After all I'm not using composite indexes.

When I run the app, the active items are now ranked according to "categoryNumber", as intended. BUT the switch buttons of all items have become irresponsive. Checking in Firebase, the "1" indeed has nowhere been changed to "0", after trying to toggle the switches.

How is it possible that a switch button in the items' recycler cards becomes irresponsive after adding a simple index ordering expression to the query?

Jonathan
  • 63
  • 6

1 Answers1

0

Such a query:

queryActive = collectionRef
        .whereEqualTo("activeStatus",1)
        .orderBy("categoryNumber", Query.Direction.ASCENDING);

Indeed requires an index. So once you run the code, check the logcat of Android Studio for the link. If somehow you don't find it, create the index manually in the Firebase console.

Besides that, in my opinion, it makes more sense to set the type of the activeStatus field to be Boolean rather than a number. So the above query looks better like this:

queryActive = collectionRef
        .whereEqualTo("activeStatus", true) //
        .orderBy("categoryNumber", Query.Direction.ASCENDING);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks, again, Alex. I had rewritten the entire app so that it avoids using "composite indices" (I understand, that means ordering along more than one field), as these indeed have to be manually enabled in the collection, in Firebase itself. Are you saying that a combination of a whereEqualTo() and a single orderBy() also constitute a "composite index", and therefore requires manual indexing? – Jonathan Aug 05 '22 at 08:46
  • Yes, that combination requires an index. – Alex Mamo Aug 05 '22 at 08:49
  • Hey Jonathan. Can I help you with other information? – Alex Mamo Aug 12 '22 at 07:54