本文介绍了如何在firebase Android中搜索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Firebase 中按如下方式保存数据:

I am saving data as follows in the Firebase:

我想查找标题中包含#Yahoo 的所有记录.对此的确切查询是什么?

I want to find all records that have #Yahoo in their title. What will be the exact query for that?

我对创建的随机密钥感到困惑.我不知道如何处理这个,所以我把它张贴在这里.

I am confused with the random key created. I am not sure how to handle this so i am posting it here.

Firebase firebase = new Firebase(Constants.FIREBASE_URL_USER_TASKS).child(Utils.encodeEmail(unProcessedEmail));
        Query queryRef = firebase.orderByKey().startAt("#" + mHashTag).endAt("#" + mHashTag + "uf8ff");

        queryRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                mTasksList.clear();
                mAdapter.notifyDataSetChanged();
                for (DataSnapshot task : dataSnapshot.getChildren()) {
                    mTasksList.add(task.getValue(TaskModel.class));
                }
                mAdapter.notifyItemRangeInserted(0, mTasksList.size());
                mSwipeToRefresh.post(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeToRefresh.setRefreshing(false);
                    }
                });
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                mSwipeToRefresh.post(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeToRefresh.setRefreshing(false);
                    }
                });
            }
        });

推荐答案

您不能搜索标题包含 #Yahoo 的任何项目.看:如何执行sqlLIKE"在 Firebase 上操作?

You cannot search for any item whose title contains #Yahoo. See: How to perform sql "LIKE" operation on firebase?

然而,您可以搜索标题开头的项目#Yahoo:

You can however search items whose title begins with #Yahoo:

Firebase firebase = new Firebase(Constants.FIREBASE_URL_USER_TASKS).child(Utils.encodeEmail(unProcessedEmail));

Query queryRef = firebase.orderByChild("title").startAt("#" + mHashTag)

为了使这项工作顺利进行,您必须将 索引添加到您的Firebase 规则:

To make this work well, you have to add an index to your Firebase rules:

"rules": {
  "userTasks": {
    "$email": {
      ".indexOn": "title" // index tasks in their title property
    }
  }
}

这篇关于如何在firebase Android中搜索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:24