Unexpected Behavior When Query by .whereArrayContains()

后端 未结 1 1176
刺人心
刺人心 2020-12-04 03:37

I have a RecyclerView that utilizes the FireaseUI and orders all objects from the \"Polls\" node by the \"timestamp\" field (sequentially).

New Fragment - .onViewCre

相关标签:
1条回答
  • 2020-12-04 04:23

    This is happening because when you are using whereArrayContains() and orderBy() methods in the same query, an index is required. To use one, go to your Firebase Console and create it manually or if you are using Android Studio, you'll find in your logcat a message that sounds like this:

    W/Firestore: (0.6.6-dev) [Firestore]: Listen for Query(products where array array_contains YourItem order by timestamp) failed: Status{code=FAILED_PRECONDITION, description=The query requires an index. You can create it here: ...

    You can simply click on that link or copy and paste the url into a web broswer and you index will be created automatically.

    Why is this index needed?

    As you probably noticed, queries in Cloud Firestore are very fast and this is because Firestore automatically creates an indexes for any fields you have in your document. When you need to order your items, a particular index is required that should be created as explained above. However, if you intend to create the index manually, please also select from the dropdown the corresponding Array contains option, as in the below image:

    enter image description here

    | improve this answer | |
    0 讨论(0)
提交回复
热议问题