gpt4 book ai didi

java - Cloud Firestore 查询 - 您只能对 null 执行相等比较

转载 作者:行者123 更新时间:2023-11-30 00:06:07 25 4
gpt4 key购买 nike

我正在尝试通过在 Cloud Firestore 中存储一个代表最小纪元的值来创建动态查询。我们称这个值为“filter_value”:

enter image description here

我通过从 .onStart() 调用成功读取了这个值,

        mStoreBaseRef.collection("epoch_filter").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot querySnapshot, FirebaseFirestoreException e) {
for (DocumentSnapshot z : querySnapshot){
Epoch epoch = z.toObject(Epoch.class);
filterEpochValue = (long) epoch.getFilter_value();
Log.v("X_VALUE", filterEpochValue.toString());
}
}
});

我已记录该值并确认已成功读取。

然后我有一个民意调查集合,每个民意调查都有一个纪元。我想使用上面的过滤器变量根据它们各自的纪元对这些民意调查执行查询,这样我只能查看大于我的“filter_value”的民意调查。

enter image description here

我正在使用以下查询:

        Query queryStore = FirebaseFirestore.getInstance()
.collection("Polls")
.whereGreaterThan("epoch", filterEpochValue)
.orderBy("vote_count");

FirestoreRecyclerOptions<Poll> storeOptions = new FirestoreRecyclerOptions.Builder<Poll>()
.setQuery(queryStore, Poll.class)
.build();

并收到以下信息:

 java.lang.IllegalArgumentException: Invalid Query. You can only perform equality comparisons on null (via whereEqualTo()).
at com.google.android.gms.internal.zzenx.zza(Unknown Source)
at com.google.firebase.firestore.Query.zza(Unknown Source)
at com.google.firebase.firestore.Query.whereGreaterThan(Unknown Source)
at com.troychuinard.fanpolls.Fragment.TrendingFragment.onStart(TrendingFragment.java:181)
at android.support.v4.app.Fragment.performStart(Fragment.java:2287)

最佳答案

您没有显示其余代码,但我怀疑您在构建查询时没有等待快照监听器回调完成。 addSnapshotListener 方法是异步,这意味着它会在结果准备好之前立即返回。你必须为此做好计划。这意味着您不应继续执行依赖于 filterEpochValue 的查询,直到该值可用为止。它第一次可用是在您的回调中,因此您应该从那里开始查询。

Please read here详细了解为什么 Firebase API 是异步的以及对它们的期望。

关于java - Cloud Firestore 查询 - 您只能对 null 执行相等比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48977687/

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