正确排序从最新到最旧的节点 [英] Correctly sort nodes from newest to oldest

查看:187
本文介绍了正确排序从最新到最旧的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase实时数据库提供的数据来使用 FirebaseRecyclerAdapter 来膨胀 RecyclerView

我开始按节点 date 对节点进行排序,该节点被设置为 ServerValue.TIMESTAMP 。我为要将 date 值排序的节点的父节点添加了一个 indexOn 属性到Firebase数据库规则。

 parent-node:{
.read:auth!= null,
.write:auth!= null,
.indexOn:date,
...
},

这工作正常,但新的节点添加到我的 RecyclerView 的末尾。作为 FirebaseRecyclerAdapter 及其 FirebaseArray 添加包含 ChildEventListener ,这意味着数据从最旧到最新

我设法通过使用负值 ServerValue.TIMESTAMP

  private void prepareUpload(){
// mDatabase是对Firebase数据库根目录的引用
final DatabaseReference timestampReference = mDatabase.child(timestamp);
final String timestampKey = timestampReference.push()。getKey();
timestampReference.child(timestampKey).setValue(ServerValue.TIMESTAMP);
timestampReference.child(timestampKey).addValueEventListener(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){$ b $ if(dataSnapshot.getValue()!= null ){
if(!(Long.parseLong(dataSnapshot.getValue()。toString())<0)){
timestampReference.child(timestampKey).setValue(0 - Long.parseLong(dataSnapshot .getValue()。toString()));
} else {
upload(/ *开始在这里用新的时间戳上传(这只是一个虚拟方法)* /);
timestampReference。 child(timestampKey).removeValue();
}
}
}
$ b $ @覆盖
public void onCancelled(DatabaseError databaseError){
Log.e(TAG,databaseError.getMessage());
}
});

$ / code $


现在我要排序的节点看起来像这样。

$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $$$$$$旧,
...
},
-K_tmjVqTUcKXHaQDphk:{
date:-1483807061979,
debug:newer,

$ b $ -K_uC-AJIvDOuBzhJ3JJ:{
date:-1483813945897,
debug:newest,
...


code
$ b $ p $它们按从最新到最旧排序并按我的意愿添加到我的 RecyclerView 的顶部。



回到我个人的问题:


  • 这是排序从最新到最旧的节点的正确方法吗?这对我来说似乎是一个很大的解决方法。

  • 我错过了什么?



b
编辑:我刚刚看到我的 prepareUpload()方法无用地将负值发送到数据库,只是为了再次接收它..我会改变这个来计算客户端的负面价值。请忽略这一点。

解决方案

不,你不会错过任何东西。是的,这是一个从最新到最旧的排序节点的正确方法。



这只是firebase的工作方式。很多时候,您可能觉得自己正在采取一个很大的解决方法,而且您认为必须有一个更简单的方法来做到这一点,但这可能不会。确实是一个很好的平台来建立你的应用程序。但是它还有一些需要改进的地方,比如实时数据库。我的意思是,没有办法正确查询你的数据。你可能可以做一些基本的过滤和排序,但如果你想做的不止这些,你自己做。



为了帮助我们,他们已经创建了针对SQL开发人员系列的Firebase数据库,您可以在链接。这些系列解释说,没有适当的/默认的方式来构建您的Firebase数据。每当你开始一个不同的项目时,你总是要花几分钟的时间集体讨论如何构建数据,以简化和减少查询来获得更好的性能。



<我希望有一天Google可以在这个数据服务中实现更多的搜索和过滤功能。


I am using a FirebaseRecyclerAdapter to inflate a RecyclerView with data provided by the Firebase Realtime Database.

I began sorting the nodes by their child date which was set to be the value of ServerValue.TIMESTAMP. I added a indexOn property to the parent node of the nodes I want to sort with the value date to the Firebase Database Rules.

"parent-node": {
  ".read": "auth != null",
  ".write": "auth != null",
  ".indexOn" : "date",
  ...
}, 

This worked fine but newer nodes were added to the end of my RecyclerView. As the FirebaseRecyclerAdapter and its FirebaseArray add nodes with a ChildEventListener, it means that the data was sorted from oldest to newest.

I managed to reverse this by using the negative value of ServerValue.TIMESTAMP.

private void prepareUpload() {
    //mDatabase is a reference to the root of the Firebase Database
    final DatabaseReference timestampReference = mDatabase.child("timestamp");
    final String timestampKey = timestampReference.push().getKey();
    timestampReference.child(timestampKey).setValue(ServerValue.TIMESTAMP);
    timestampReference.child(timestampKey).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.getValue() != null) {
                if (!(Long.parseLong(dataSnapshot.getValue().toString()) < 0)) {
                    timestampReference.child(timestampKey).setValue(0 - Long.parseLong(dataSnapshot.getValue().toString()));
                } else {
                    upload(/*Starting upload with new timestamp here (this is just a dummy method)*/);
                    timestampReference.child(timestampKey).removeValue();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e(TAG, databaseError.getMessage());
        }
    });
}

Now the nodes I want to sort look like this.

{
  "-K_tlLWVO21NXUjUn6ko" : {
    "date" : -1483806697481,
    "debug" : "old",
    ...
  },
  "-K_tmjVqTUcKXHaQDphk" : {
    "date" : -1483807061979,
    "debug" : "newer",
    ...
  },
  "-K_uC-AJIvDOuBzhJ3JJ" : {
    "date" : -1483813945897,
    "debug" : "newest",
    ...
  }
}

They are sorted from newest to oldest and get added to the top of my RecyclerView exactly as I wanted it to be.

Coming to my personal question:

  • Is this a proper way to sort nodes from newest to oldest? This seems like a big workaround to me.
  • Am I missing something?

Thanks in advance!

Edit: I just saw that my prepareUpload() method is uselessly sending the negative value to the database again, just to receive it once again afterwards.. I will change this to calculate the negative value on client side. Please ignore this.

解决方案

No, you are not missing anything. And yes, that's a proper way to sort nodes from newest to oldest.

That's just the way firebase works. Many times, you might feel like you're doing a big workaround and you think "There must be a simpler way to do this", but there's probably not.

Firebase is indeed a great platform to build your application on. But it still has a few things to improve, like the realtime database. I mean, there is no way to query your data properly. You may be able to do some basic filtering and sorting, but If you want to do more than that, you're on your own.

To help us with that, they have created the Firebase Database for SQL Developers Series which you can watch on this link. Those series explain that there is no proper/default way to structure your data on Firebase. Everytime you start a different project, you'll always have to spend some minutes brainstorming about how you can structure the data in a way that it simplifies and reduces queries for better performance.

I hope Google some day implements more searching and filtering capabilities in this data service.

这篇关于正确排序从最新到最旧的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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