【问题标题】:OrderByKey works fine but OrderByChild gives unexpected results in firebase realtime databaseOrderByKey 工作正常,但 OrderByChild 在 firebase 实时数据库中给出了意想不到的结果
【发布时间】:2018-06-07 14:23:00
【问题描述】:

我正在尝试使用 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 方法中收到的第一个孩子的密钥。

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

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

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

【问题讨论】:

  • 在查看来自OrderByChild 查询的日志时,endAtKey 是 uid 还是时间戳?
  • @RosárioPereiraFernandes 它是时间戳。哦,我想我对您的评论有疑问,谢谢,我将尝试使用 Uid 进行操作。 :)
  • @RosárioPereiraFernandes 我再次检查了我在之前的评论中说错了。我正在使用的是 Uid。所以我认为我应该用 timeStamp 来做,因为这就是我们要查询的。我会再次检查时间戳。
  • @RosárioPereiraFernandes 非常感谢。我使用了时间戳,一切都很顺利。请为此写一个答案,我会接受。我非常感谢你。 :)

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2019-05-19
    • 2017-01-05
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    相关资源
    最近更新 更多