Ordering data does not actually order anything

浪尽此生 提交于 2021-02-02 09:14:12

问题


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
   }
}

This is how the data should be.

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

Kind regards, Namanix


回答1:


The result you show is a JSON object with other objects in there. JSON objects are never ordered as far as I know, only retrievable by key. JSON Arrays would be, but it doesn't look like you get that. When you would change this to an array the document IDs would have to be inside the document instead of being the object key. My guess would be that 'orderBy' is meant to be used for example to limit the number of items you get for pagination. Than you can order by timestamp, limit the number of items to 20 and search from the last timestamp you got.

I think if you want to order them I would put them in a new list of objects which can be ordered.




回答2:


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.

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

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.

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




回答3:


I now temporarily fixed it by doing this in my callback. But this feels like a very bad way to fix it. I hope to get some thoughts on this.

static void _messagesSubscriptionCallback(Event event) {
    _messagesList.clear();
    _messages.clear();
    _messages = event.snapshot.value;
    _messagesList = _messages.keys.toList();
    _messagesList.sort((a, b) {
        return b.compareTo(a) ;
    });
    onMessagesChange();
}


来源:https://stackoverflow.com/questions/49829127/ordering-data-does-not-actually-order-anything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!