java - 通过 .whereArrayContains() 查询时的意外行为

标签 java android firebase google-cloud-firestore firebaseui

我有一个 RecyclerView,它利用 FireaseUI 并按“时间戳”字段(顺序)对“投票”节点中的所有对象进行排序。

新 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);

                }
            });
        }

我的应用程序中有另一个布局订阅了同一个节点,而是通过“followers”查询,然后通过“timestamp.:

以下 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”查询时,我没有得到同样的行为,我很好奇为什么。这些项目仅在我重新启动应用程序时重新出现:

enter image description here

编辑:

我注释掉了下面这行:

//.whereArrayContains("关注者", mUserId)

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

最佳答案

发生这种情况是因为当您在同一查询中使用 whereArrayContains()orderBy() 方法时,index是必须的。要使用一个,请转到您的 Firebase Console并手动创建它,或者如果您使用的是 Android Studio,您会在 logcat 中找到一条听起来像这样的消息:

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 复制并粘贴到网络浏览器中,系统就会自动创建您的索引。

Why is this index needed?

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

enter image description here

关于java - 通过 .whereArrayContains() 查询时的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53454763/

相关文章:

android - 启动genymotion时出现问题

java - 更改java代码/android中的微调器文本颜色?

android - 为什么我们需要在 Firebase SDK 的底部应用 google-services 插件?

Javah:错误:无法访问android.support.v7.app.ActionBarActivity

java - 我正在编译一个电子商务应用程序,但我在 gradle 控制台中收到以下错误

ios - Xcode FIRRemoteConfig 构建错误

firebase - Flutter中创建文档后返回新创建的Document ID

firebase - 使用 firebase API 创建项目时出现 403(作为所有者用户,而不是服务帐户)

java - MongoDB 从 BasicDBObject (Java) 中提取值

java - 如果默认接受子类对象,为什么要在泛型中使用有界类型参数?