在负面时间戳中按日期排序在Firebase中不起作用 [英] Order by date in negative timestamp is not working in Firebase

查看:166
本文介绍了在负面时间戳中按日期排序在Firebase中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的查询:

  q = FirebaseDatabase.getInstance()。getReference()。child(products)。orderByChild ( 时间戳); 
q.addValueEventListener(new ValueEventListener(){
@Override
public void onCancelled(DatabaseError databaseError){
}

@Override
public void onDataChange(DataSnapshot dataSnapshot){
Log.d(datasnapshot,dataSnapshot.toString());

这个值在我的Firebase数据库中的顺序相同。



注意:我使用负值方法 {-1 *新的Date().getTime();} 来存储时间戳。

这个值在我的Firebase数据库中的顺序相同:



Ke7K4vXyEt4gKnjf68H

时间戳:-1488345920790

Ke7KB2F1UKh8LoWcCY3

时间戳:-1488345945825

KeHlQIBnOE3diP8Wksh

时间戳:-1488521122407

KeI9nJtt4eg5Vd0rcAv

时间戳:-1488527774123

KeWwWW_ezG-cjmF8TNO

时间戳: -1488775680799

KeX7G0WOVidrMBbuGiT

时间戳:-1488778758960

Keg3mtNpyKStpVMMvRm

时间戳:-1488945623757

Keg4fN3rAN7uy5weFJz

时间戳:-1488945855094

下面的命令是我在打印datasnapshot时得到的:

KeX7G0WOVidrMBbuGiT

时间戳:-1488778758960

Keg3mtNpyKStpVMMvRm

时间戳:-1488945623757

Keg4fN3rAN7uy5weFJz

时间戳:-1488945855094

Ke7K4vXyEt4gKnjf68H

时间戳:-1488345920790 < br>
KeWwWW_ezG-cjmF8TNO

时间戳:-1488775680799

KeHlQIBnOE3diP8Wksh

时间戳:-1488521122407

Ke7KB2F1UKh8LoWcCY3

时间戳:-1488345945825

KeI9nJtt4eg5Vd0rcAv

时间戳:-1488527774123

解决方案当您对Firebase数据库执行查询时,它会为每个匹配的节点返回三条信息:


  1. 值该节点

  2. 该节点的键

  3. 该节点相对于结果中其他节点的顺序


    打印快照时,实际上是打印查询结果的 Map< String,Object> 。在地图中,只有两个信息空间:每个节点的密钥和该节点的值。因此,当你打印结果时,节点的顺序会丢失。

    因此,你应该使用内置的<$ c $来遍历结果的子元素c> getChildren()快照方法:

    preublic $ onDataChange(DataSnapshot dataSnapshot){$ (DataSnapshot child:dataSnapshot.getChildren()){
    Log.d(datasnapshot,child.getKey()+:+ child.getValue());






    $ b

    如果你想保留在列表中(以便保持顺序),我建议你保留一个快照列表:

    公共无效onDataChange(DataSnapshot dataSnapshot){
    List< DataSnapshot>

      list = new LinkedList< DataSnapshot>(); (DataSnapshot child:dataSnapshot.getChildren()){
    Log.d(datasnapshot,child.getKey()+:+ child.getValue());
    list.add(child);
    }
    }


    Query used:

    q = FirebaseDatabase.getInstance().getReference().child("products").orderByChild("timestamp");
    q.addValueEventListener(new ValueEventListener() {
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d("datasnapshot", dataSnapshot.toString());
    

    This value is in my Firebase database in same order.

    Note: I am using negative value approach { -1 * new Date().getTime();} to store timestamp.

    This value is in my Firebase database in same order:

    Ke7K4vXyEt4gKnjf68H
    timestamp: -1488345920790
    Ke7KB2F1UKh8LoWcCY3
    timestamp: -1488345945825
    KeHlQIBnOE3diP8Wksh
    timestamp: -1488521122407
    KeI9nJtt4eg5Vd0rcAv
    timestamp: -1488527774123
    KeWwWW_ezG-cjmF8TNO
    timestamp: -1488775680799
    KeX7G0WOVidrMBbuGiT
    timestamp: -1488778758960
    Keg3mtNpyKStpVMMvRm
    timestamp: -1488945623757
    Keg4fN3rAN7uy5weFJz
    timestamp: -1488945855094

    Below order is what I get when I print the datasnapshot:

    KeX7G0WOVidrMBbuGiT
    timestamp: -1488778758960
    Keg3mtNpyKStpVMMvRm
    timestamp: -1488945623757
    Keg4fN3rAN7uy5weFJz
    timestamp: -1488945855094
    Ke7K4vXyEt4gKnjf68H
    timestamp: -1488345920790
    KeWwWW_ezG-cjmF8TNO
    timestamp: -1488775680799
    KeHlQIBnOE3diP8Wksh
    timestamp: -1488521122407
    Ke7KB2F1UKh8LoWcCY3
    timestamp: -1488345945825
    KeI9nJtt4eg5Vd0rcAv
    timestamp: -1488527774123

    解决方案

    When you execute a query against the Firebase Database, it returns three pieces of information for each matching node:

    1. the value of that node
    2. the key of that node
    3. the order of that node relative to the other nodes in the result

    When you print the snapshot, you're actually printing a Map<String, Object> of the query result. And in a map there is only space for two pieces of information: the key of each node and the value of that node. So the order of the nodes is lost when you print the result.

    For that reason you should iterate over the children of the result with the built-in getChildren() method of the snapshot:

    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child: dataSnapshot.getChildren()) {
            Log.d("datasnapshot", child.getKey()+": "+child.getValue());
        }
    }
    

    And with this you will see the children in the order of their timestamp.

    If you want to keep them in a list (so that the order is maintained), I recommend simply keeping a list of snapshots:

    public void onDataChange(DataSnapshot dataSnapshot) {
        List<DataSnapshot> list = new LinkedList<DataSnapshot>();
        for (DataSnapshot child: dataSnapshot.getChildren()) {
            Log.d("datasnapshot", child.getKey()+": "+child.getValue());
            list.add(child);
        }
    }
    

    这篇关于在负面时间戳中按日期排序在Firebase中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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