如何根据创建时间对 Firebase 数据进行排序? [英] How to sort Firebase data according to Time Created?

查看:34
本文介绍了如何根据创建时间对 Firebase 数据进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

大家好,我已经使用 Query 从应用中的 Firebase 实时数据库加载了一些数据.

<块引用>

这是简单的代码:

 Query query = databaseReference.orderByChild("name").startAt(data).endAt(data + "\uf8ff");options = new FirebaseRecyclerOptions.Builder().setQuery(query, HomeModel.class).build();

<块引用>

我的数据库结构很简单;

M38dJdquf4jE(随机密钥)名称:约翰·多伊"图片:图片链接"

<块引用>

由于orderByChild(name"),每当有新数据插入数据库时​​,都不是根据插入数据的时间.相反,它以随机顺序将数据插入到 recyclerView 中.

<块引用>

我该如何修复它,以便最后插入的数据始终位于 recyclerView 中的最后,而不是以随机方式插入.

解决方案

因为orderByChild("name"),每当有新数据插入数据库时​​,都不是按照时间

当您使用时:

.orderByChild("name")

这意味着来自数据库的结果将根据name"中存在的值进行排序.属性,并且不是根据时间".

<块引用>

相反,它以随机顺序将数据插入到 RecyclerView 中.

如上所述,这绝对不是随机顺序.如果需要根据时间"获取结果的加法,那么你有两个选择.

例如,如果M38dJdquf4jE"是由push()"提供的按键.方法,您可以使用 Query 的 orderBykey() 方法,其中:

<块引用>

创建一个查询,其中子节点按其键进行排序.

您将得到有序的结果,因为按下的键包含时间组件.

如果这些键由push()"提供,方法,要根据时间组件进行排序,那么您应该添加一个时间戳"类型的属性,如我在以下帖子中的回答中所述:

一旦你有了新的属性,你就可以使用:

.orderByChild(时间戳")

让结果按升序排列.但是,如果您需要降序,则可以使用我在以下帖子中的回答:

Hello Everyone, I have loaded some data from Firebase Realtime Database in the App using Query.

This is the Simple code :

  Query query = databaseReference.orderByChild("name").startAt(data).endAt(data + "\uf8ff");

  options = new FirebaseRecyclerOptions.Builder<HomeModel>().setQuery(query, HomeModel.class).build();

My database structure is simple ;

M38dJdquf4jE(the random key)
name : "John Doe"
Image : "image link"

Because of orderByChild("name"), whenever a new Data is inserted in the Database is not according to the Time the data is inserted. Instead, it inserts the data in the recyclerView in a Random Order.

How can I fix it, so that the Last data inserted is always at the last in the recyclerView and not inserted in a random manner.

解决方案

Because of orderByChild("name"), whenever a new Data is inserted in the Database is not according to the Time

When you are using:

.orderByChild("name")

It means that the results that are coming from the database will be ordered according to the values that exist in the "name" property, and not according to a "time".

Instead, it inserts the data in the RecyclerView in a Random Order.

As said above, that's definitely not a random order. If you need to get the results according to the "time" of the addition, then you have two options.

If, for example, the "M38dJdquf4jE" is a pushed key that is provided by the "push()" method, you can use Query's orderBykey() method, which:

Creates a query in which child nodes are ordered by their keys.

You'll get the results ordered because the pushed keys contain a time component.

If those keys are not provided by the "push()" method, to have an order according to a time component, then you should add a property of type "timestamp", as explained in my answer from the following post:

Once you have that new property in place, you can use:

.orderByChild("timestamp")

To have the results ordered ascending. If you need however a descending order, then you can use my answer from the following post:

这篇关于如何根据创建时间对 Firebase 数据进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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