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

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

问题描述

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

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

亲切的问候,纳曼尼克斯

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天全站免登陆