订购数据实际上不订购任何东西 [英] Ordering data does not actually order anything

查看:41
本文介绍了订购数据实际上不订购任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Firebase数据库中获取消息的数据集,并希望消息按添加/时间戳排序.但是由于某种原因,实际上没有使用我在代码中输入的命令.我尝试做这两件事.

I'm trying to get a dataset of messages out of my firebase database and want the messages sorted by added/timestamp. But for some reason no orderby I put in the code is actually used. I tried doing these 2 things.

_messagesRef = FirebaseDatabase.instance.reference().child('messages/'+key);
_membersSubscription = _messagesRef
    .orderByChild('timestamp')
    .onValue//On valuechange
    .listen((Event event) => _messagesSubscriptionCallback(event)); 

 _messagesRef = FirebaseDatabase.instance.reference().child('messages/'+key);
 _membersSubscription = _messagesRef
    .orderByKey()
    .onValue//On valuechange
    .listen((Event event) => _messagesSubscriptionCallback(event)); 

两者都给了我相同的数据集,该数据集没有按时间戳记或在回调中进行排序.我在下面添加了输出

Both give me back the same dataset that is not ordered by timestamp or key in the callback. I've added the output underneath

{  
   -LA-Aw6crEAV53LxdujP:{  
      sender:1508,
      message:test s9 2,
      timestamp:1523642778089
   },
   -LA-Arby61T1UG5URMn6:{  
      sender:1508,
      message:test s9,
      timestamp:1523642759679
   },
   -LA-AyC7F8KAqceZBE3j:{  
      sender:1509,
      message:test emu 1,
      timestamp:1523642786632
   },
   -LA22WiUfL2tbh7-OjtM:{  
      sender:1508,
      message:Blaj,
      timestamp:1523690904480
   },
   -LA-B29RRXbdOPT1mG7m:{  
      sender:1508,
      message:tesy3,
      timestamp:1523642806940
   }
}

这就是应该的数据.

我希望有人可以帮助我解决这个问题.我想我可能会误解订购数据如何与Firebase一起使用

I hope someone can help me with this issue. I think I might misunderstand how ordering data works with Firebase

亲切的问候,Namanix

Kind regards, Namanix

推荐答案

最有可能(很难确认是否没有看到 _messagesSubscriptionCallback ),当您从将Firebase转换为普通的JSON对象(正如Rene所说),该对象没有任何已定义的顺序.

Most likely (it's hard to be sure without seeing _messagesSubscriptionCallback) you're throwing the ordering information away when you convert the data from Firebase into a plain JSON object, which (as Rene also says) doesn't have any defined order.

但是您从Firebase请求的数据确实具有订购信息,您只需要确保不要删除它即可.

But the data your request from Firebase does have ordering information, you just have to be sure to not drop it.

通常的方法是监听 onChildAdded 而不是 onValue .这样,Firebase就会依次为每个孩子调用 onChildAdded ,并按照您请求的顺序进行操作.

The usual way to do this is to listen for onChildAdded instead of onValue. With that Firebase will invoke onChildAdded for each child in turn, and it will do so in the order you requested.

另请参见此示例,例如 FirebaseAnimatedList 在这里做什么.

Also see this example and for example what FirebaseAnimatedList does here.

这篇关于订购数据实际上不订购任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆