android - OrderByKey 工作正常,但 OrderByChild 在 firebase 实时数据库中给出了意想不到的结果

标签 android sorting firebase firebase-realtime-database android-recyclerview

我正在尝试使用 firebase 实时数据库对数据进行排序和过滤。在一种情况下,我想使用 OrderByKey(因为我首先想要新问题并使用推送功能生成键)对数据进行排序,在另一种情况下,我使用 OrderByChild(因为我想使用时间戳进行排序,在这种情况下我不t 使用 push 但我使用用户的 uid 作为键)。

在这两种情况下,我首先想要最新的事物/人(问题/用户),所以我想按降序排序(在客户端我什至颠倒了它)。所以这就是我在第一次查询后的查询方式。第一个查询在下面显示的查询中没有 endAt(endAtKey) 部分。

对于 OrderByKey:

 ref.child("questions").orderByKey().endAt(endAtKey).limitToLast(10);


   //data at questions node looks like this
    {
  "-L1CbnCkXJiRT9MKpoP1" : {
    "mCorrectAnswer" : 1,
    "mOptionA" : "ueud",
    "mOptionB" : "h",
    "mOptionC" : "h",
    "mOptionD" : "h",
    "mProblem" : "4shsudududududududurududu"
  },
  "-L1Dd9x6ws5di2rf8yay" : {
    "mCorrectAnswer" : 1,
    "mOptionA" : "sh",
    "mOptionB" : "shsh",
    "mOptionC" : "hehs",
    "mOptionD" : "hssh",
    "mProblem" : "12ffsnsjsjdbxbddhdbdbddjebddb"
  }

对于 OrderByChild:

 mDatabaseReference.child("user_list").orderByChild("mTimeStamp")
                        .endAt(endAtKey).limitToLast(10);



     //data at user_list reference looks like this
     { 
    "VH78axip1bXN9dktsF4Az0oBche2": 
     "mTimeStamp": "1508782492973", 
     "mUserName": "Satyam Kumar" 
     }, 
    "2MedW9KSYTcB8I1qp0xcoNuwPEf1": 
     "mTimeStamp": "1508823085614", 
     "mUserName": "Thirunagari Yeshwanth" 
     }

我想用 10 组数据填充 RecyclerView,所以在 onDataChange 中,我保存在 onDataChange 方法中收到的第一个 child 的 key 。

     if (countCopy == 1) {  // while iterating through children this variable tells 
//the index of the loop. 

                                endAtKey = currentKey;
                                Log.e("endKey", endAtKey);
                            }

现在我面临的问题是使用上面的查询后续获取数据我收到 endAtKey 罚款(收到的数据也是预期的) OrderByKey查询,但 endAtKey 对于 OrderByChild 查询始终相同,并且接收到的数据始终相同。 我该如何解决这个问题?

最佳答案

您的 OrderByChild 查询的问题是您获得的 endAtKey 是一个键,而不是 timestamp。请记住,OrderByChild 用于查询时间戳属性,因此您的 endAtKey 必须是时间戳而不是键。

关于android - OrderByKey 工作正常,但 OrderByChild 在 firebase 实时数据库中给出了意想不到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47983720/

相关文章:

python - 从python中的特定索引值排序列表

r - 忽略数字符号对 R 中的列进行排序

firebase - 是否可以在没有 google-services.json 配置文件的情况下配置 Firebase 分析?

firebase - 使用 firebase 火焰计划安全吗?

c - 快速排序实现在 C 中不排序

javascript - 如何从嵌套函数中检索值

ANDROID_HOME 未设置且 "android"命令不在您的 PATH Phonegap 中

android - 在 Kotlin 中使用 MVVM 实现两种方式的数据绑定(bind) WebView

android - fragment 中的 inflaterException

android - Titanium Studio 3.2.0 无法使用 Genymotion 2.0.3 进行调试