gpt4 book ai didi

java - 如何使用 FirestoreRecyclerAdapter 进行复杂查询?

转载 作者:行者123 更新时间:2023-11-29 23:19:17 26 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,我需要对我的 Firestore 数据库进行复杂的查询并创建一个 FirestoreRecyclerAdapter。要创建适配器,我需要一个接受输入整个查询的 FirestoreRecyclerOptions 对象。阅读文档后,我无法在查询中对不同参数使用方法 whereGreaterThanwhereLessThanoderBy 等。例如,如何从数据库中获取年龄大于/小于 AND 体重大于/小于的用户?

例如,我的 firestore 数据库中的文档结构是:

Users --->UserID--->userName(String)
--->userAge(number)
--->userHeight(number)
--->userWeight(number)

FirebaseFirestore db;

db = FirebaseFirestore.getInstance();

RecyclerView recyclerView;

recyclerView = (RecyclerView)findViewById(R.id.recyclerViewID);
.
.
.

Query query = db.collection("Users").//the complex query that i need

FirestoreRecyclerOptions<User> options = new FirestoreRecyclerOptions.Builder<User>()
.setQuery(query, User.class)
.build();

adapter = new UsersAdapter(options, this);//get in input options and the context

recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);

编辑:我上一条回答 1 的评论中的一个可能的解决方案

最佳答案

有一些query limitations谈到 Firestore:

Query limitations

Cloud Firestore does not support the following types of queries:

  • Queries with range filters on different fields, as described in the previous section.

因此,您无法在不同属性上使用 whereGreaterThan()whereLessThan() 方法在范围过滤器上查询数据库并将其传递给适配器。

编辑:

此问题的一个可能解决方案是在客户端过滤您的记录两次。首先使用第一个属性查询数据库,然后使用第二个属性查询数据库。不幸的是,您无法一次性实现这一目标。

编辑2:

解决方案是使用第一个查询查询数据库,获取相应的元素,使用第二个查询再次查询数据库并获取相应的元素,然后在客户端合并结果。现在数据库中的元素被过滤了两次。将这些元素的列表传递给适配器,仅此而已。请注意,在使用此解决方案时,您不能再使用 Firebase-UI 库,但您可以通过这种方式对项目进行两次过滤。

关于java - 如何使用 FirestoreRecyclerAdapter 进行复杂查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54707050/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com