通过.whereArrayContains()查询时发生意外行为 [英] Unexpected Behavior When Query by .whereArrayContains()

查看:110
本文介绍了通过.whereArrayContains()查询时发生意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RecycleerView,该视图利用FireaseUI并通过时间戳"字段(依次)从投票"节点中订购所有对象.

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

新片段-.onViewCreated()

New Fragment - .onViewCreated()

 Query queryStore = FirebaseFirestore.getInstance()
            .collection(POLLS_LABEL)
            .orderBy("timestamp", Query.Direction.ASCENDING);
    //Cloud Firestore does not have any ordering; must implement a timestampe to order sequentially

    FirestoreRecyclerOptions<Poll> storeOptions = new FirestoreRecyclerOptions.Builder<Poll>()
            .setQuery(queryStore, Poll.class)
            .build();
    mFirestoreAdaper = new FirestoreRecyclerAdapter<Poll, PollHolder>(storeOptions) {
        @Override
        protected void onBindViewHolder(@NonNull final PollHolder holder, final int position, @NonNull Poll model) {
            holder.mPollQuestion.setText(model.getQuestion());
            String voteCount = String.valueOf(model.getVote_count());
            //TODO: Investigate formatting of vote count for thousands
            holder.mVoteCount.setText(voteCount);
            Picasso.get()
                    .load(model.getImage_URL())
                    .fit()
                    .into(holder.mPollImage);
            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent toClickedPoll = new Intent(getActivity(), PollHostActivity.class);
                    String recyclerPosition = getSnapshots().getSnapshot(position).getId();
                    Log.v("Firestore ID", recyclerPosition);
                    toClickedPoll.putExtra("POLL_ID", recyclerPosition);
                    startActivity(toClickedPoll);

                }
            });
        }

我的应用程序中有另一个布局可以订阅相同的节点,但是可以通过跟随者"然后通过时间戳"进行查询.

I have another layout in my app that subscribes to this same node, but instead queries by "followers" and then by "timestamp.:

以下片段-.onViewCreated()

Following Fragment - .onViewCreated()

  Query queryStore = FirebaseFirestore.getInstance()
            .collection(POLLS_LABEL)
            .whereArrayContains("followers", mUserId)
            .orderBy("timestamp", Query.Direction.ASCENDING);

    FirestoreRecyclerOptions<Poll> storeOptions = new FirestoreRecyclerOptions.Builder<Poll>()
            .setQuery(queryStore, Poll.class)
            .build();
    mFirestoreAdaper = new FirestoreRecyclerAdapter<Poll, PollHolder>(storeOptions) {
        @Override
        protected void onBindViewHolder(@NonNull final PollHolder holder, final int position, @NonNull Poll model) {
            holder.mPollQuestion.setText(model.getQuestion());
            String voteCount = String.valueOf(model.getVote_count());
            //TODO: Investigate formatting of vote count for thousands
            holder.mVoteCount.setText(voteCount);
            Picasso.get()
                    .load(model.getImage_URL())
                    .fit()
                    .into(holder.mPollImage);
            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent toClickedPoll = new Intent(getActivity(), PollHostActivity.class);
                    String recyclerPosition = getSnapshots().getSnapshot(position).getId();
                    Log.v("Firestore ID", recyclerPosition);
                    toClickedPoll.putExtra("POLL_ID", recyclerPosition);
                    startActivity(toClickedPoll);

                }
            });
        }

在第一种情况下,UI项在添加到Firebase中时实时填充到我的RecyclerView中.但是,当我通过".whereArrayContains"查询时,我没有得到相同的行为,并且我很好奇为什么.仅当我重新启动应用程序时,这些项目才会重新出现:

In the first scenario, UI items populate, in real-time, into my RecyclerView as they are added to Firebase. However, when I query by ".whereArrayContains," I do not get this same behavior, and I was curious as to why. The items only reappear when I restart the application:

我注释了以下内容:

//.whereArrayContains("followers",mUserId)

// .whereArrayContains("followers", mUserId)

并按预期执行行为,因此我可以将问题隔离到.whereArrayContains()查询中.这是每个片段之间的唯一区别.

and the behavior performed as expected, therefore I can isolate the issue to the .whereArrayContains() query. It is the only difference between each Fragment.

推荐答案

之所以会发生这种情况,是因为在同一查询中使用whereArrayContains()orderBy()方法时,会使用 Firebase控制台并手动创建它,或者如果您使用的是Android Studio,您会在logcat中找到一条听起来像这样的消息:

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]:侦听查询(按时间戳记数组array_contains YourItem顺序所在的产品)失败:Status {code = FAILED_PRECONDITION,description =查询需要索引.您可以在这里创建它:...

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: ...

您只需单击该链接或将URL复制并粘贴到网络浏览器中,您的索引就会自动创建.

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?

您可能已经注意到,Cloud Firestore中的查询非常快,这是因为Firestore会自动为文档中的任何字段创建索引.当您需要订购商品时,需要按照上面的说明创建一个特定的索引.但是,如果要手动创建索引,请从下拉菜单中选择相应的Array contains选项,如下图所示:

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:

这篇关于通过.whereArrayContains()查询时发生意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆