合并Firestore查询时出现重复数据 [英] Duplicate data when I merge Firestore queries
问题描述
我的后端基于Firestore构建,没有逻辑 OR
来构建查询,因此要解决此问题,我使用了多个查询并将其合并以模拟逻辑 OR
,查询请求和合并可以完美地工作,但是现在还有另一个问题,我得到重复的数据,因此,为了解决这个问题,我决定创建一个 HashSet
并将对象存储在其中,但是我仍然得到重复的数据在哈希集中
这是我合并查询的方式:
Tasks.whenAllSuccess(c).addOnSuccessListener(new BaseValueOnSuccessListener(mapper,firebaseCallback));
c是Task的集合
这是我(在自定义BaseValueOnSuccessListener侦听器中)存储数据的方式:请检查我的OnSuccess方法
公共类BaseValueOnSuccessListener< Model,Entity>实现OnSuccessListener< List< QuerySnapshot>>{私有FirebaseMapper<实体,模型>映射器//FirebaseDatabaseRepositoryCallback是一个接口私有FirebaseDatabaseRepository.FirebaseDatabaseRepositoryCallback< Model>打回来;公共BaseValueOnSuccessListener(FirebaseMapper< Entity,Model>映射器,FirebaseDatabaseRepository.FirebaseDatabaseRepositoryCallback< Model>打回来) {this.mapper =映射器;this.callback =回调;}@Override公共无效onSuccess(List< QuerySnapshot> querySnapshots){//做您需要处理的清单如果(!querySnapshots.isEmpty()){//List< Model>数据=新的ArrayList<>();设置<模型>数据=新的HashSet<>();对于(QuerySnapshot querySnapshot:querySnapshots){data.addAll(mapper.mapList(querySnapshot));}callback.onSuccess(new ArrayList< Model>(data));}}}
现在还有另一个问题,我得到重复的数据,因此,为了解决这个问题,我决定创建一个哈希集并将对象存储在其中,但是我仍然在哈希集中得到重复的数据
您得到重复的数据,因为要在 Set
中添加类型为 Model
的不同对象.集合中的每个 Model
对象在内存中都有不同的地址,这就是集合中每个对象都不同的原因. Set
是扩展Collection的接口,因此,使用此类集合时,除非您指定了对象,否则您不知道对象中的字段是否与另一个对象中的另一个字段具有相同的值.>
Set
和 List
之间有很大的区别.如果您使用的是:
List< String>list = new ArrayList<>();list.add("user2426691");list.add("user2426691");list.add("user2426691");Log.d(TAG,list.toString());
您将看到您的列表将包含三个重复的名称.但是,如果您像下面的代码行那样使用字符串的 Set
,则:
Set< String>set = new HashSet<>(list);Log.d(TAG,set.toString());
您会注意到,您的 Set
集将仅包含一个元素(没有重复项).
要解决您的问题,可以使用 List
或 Set
,但是您需要根据条件填充所需的对象集合,这些条件可能是:
if(!model.getName().equals("someName")){list.add(model);}
My backend is built on Firestore and there's no logical OR
to build my queries and so to fix this I'm using multiple queries and merging them to simulate a logical OR
, query request and merge works perfectly, but now there's another problem I get duplicate data, and so to fix this I decided to create a HashSet
and store my objects in it, but I still get duplicate data in a hashset
Here's how I merge queries:
Tasks.whenAllSuccess(c).addOnSuccessListener(new BaseValueOnSuccessListener(mapper, firebaseCallback));
c is a collections of Task
and here's how I store my data (in my custom BaseValueOnSuccessListener listener): Please check my OnSuccess method
public class BaseValueOnSuccessListener<Model, Entity> implements OnSuccessListener<List<QuerySnapshot>> {
private FirebaseMapper<Entity, Model> mapper;
// FirebaseDatabaseRepositoryCallback is an interface
private FirebaseDatabaseRepository.FirebaseDatabaseRepositoryCallback<Model> callback;
public BaseValueOnSuccessListener(FirebaseMapper<Entity, Model> mapper,
FirebaseDatabaseRepository.FirebaseDatabaseRepositoryCallback<Model> callback) {
this.mapper = mapper;
this.callback = callback;
}
@Override
public void onSuccess(List<QuerySnapshot> querySnapshots) {
// Do what you need to do with your list
if (!querySnapshots.isEmpty()) {
// List<Model> data = new ArrayList<>();
Set<Model> data = new HashSet<>();
for (QuerySnapshot querySnapshot : querySnapshots) {
data.addAll(mapper.mapList(querySnapshot));
}
callback.onSuccess(new ArrayList<Model>(data));
}
}
}
now there's another problem I get duplicate data, and so to fix this I decided to create a hashset and store my objects in it, but I still get duplicate data in a hashset
You are getting duplicate data because you are adding in your Set
, different objects of type Model
. Every Model
object in your in set has a different address in the memory and that's why every object within your set is different. The Set
is an interface which extends Collection, so when using such a collection, you cannot know if a field within an object holds the same value as another filed within another object, unless you specify it.
There is a big difference between a Set
and a List
. If you are using:
List<String> list = new ArrayList<>();
list.add("user2426691");
list.add("user2426691");
list.add("user2426691");
Log.d(TAG, list.toString());
You'll see that your list will contains three duplicate names. However, if you would have used a Set
of strings like in the following lines of code:
Set<String> set = new HashSet<>(list);
Log.d(TAG, set.toString());
You would have been noticed that the your Set
will contain only one element (no duplicates).
To solve your problem, you can either use a List
or a Set
but you need to populate the desired collection of objects based on a condition, which might be:
if(!model.getName().equals("someName")) {
list.add(model);
}
这篇关于合并Firestore查询时出现重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!