Firebase获取最后一个id - limitToLast无效 - android

时间:2016-04-14 02:48:47

标签: android firebase

我正在尝试获取我的消息节点的最后一个ID,但它没有得到最后一个。例如,我的上一个ID是12,但它总是返回项目10的ID。我的代码是这样的:

final Query query = firebase.orderByChild("messageId").limitToLast(1);
query.addListenerForSingleValueEvent(new ValueEventListener() {

   public void onDataChange(DataSnapshot dataSnapshot) { 

      Log.i("MessagesFrag","snapshot: "+dataSnapshot.getValue()+""); 

   }

   public void onCancelled(FirebaseError firebaseError) {}

});

它返回如下内容:

{10={chatId=6, time=Wed Apr 13 22:20:43 EDT 2016, message=hi, user=123, messageId=10}}

它应该返回:

{12={chatId=6, time=Wed Apr 13 22:29:02 EDT 2016, message=I am fine, user=1234, messageId=12}}

我的数据是这样的:

messages
  9:
    chatId: "6"
    message: "hi"
    messageId: "9"
    time: "Wed Apr 13 22:29:02 EDT 2016"
    user: "123"
  10:
    chatId: "6"
    message: "hello"
    messageId: "10"
    time: "Wed Apr 13 22:29:02 EDT 2016"
    user: "1234"
  11:
    chatId: "6"
    message: "how are you?"
    messageId: "11"
    time: "Wed Apr 13 22:29:02 EDT 2016"
    user: "123"
  12:
    chatId: "6"
    message: "I am fine"
    messageId: "12"
    time: "Wed Apr 13 22:29:02 EDT 2016"
    user: "1234"

除此之外,当我改变" LimitToLast"到" LimitToFirst" (应该得到第一个),它没有得到我拥有的第一个元素(节点1),它也获得了节点10。

有谁知道会出现什么问题?

1 个答案:

答案 0 :(得分:0)

我认为问题是override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.Portrait } ,因为消息是以数字方式排序的,无需再次排序。试试这个:

orderByChild