【发布时间】:2020-05-09 00:37:39
【问题描述】:
所以我在我的服务器上监控了几个对象,并且每个小时我都想将这些对象的读数发布到我的数据库中。我需要一种简单的方法来获取这些数据,以便我可以按数组中的日期对它们进行排序,轻松获取特定日期的值,并清理特定日期不再需要的旧数据。
我目前对如何构建数据有两个想法:
1) 按日期分块
objects
- objectA
- 2020
- 01
- 22
- 01
- property1 : "Value at 1 o'clock on the 22nd of Jan, 2020"
- property2 : "Value at 1 o'clock on the 22nd of Jan, 2020"
- 02
- property1 : "Value at 2 o'clock on the 22nd of Jan, 2020"
- property2 : "Value at 2 o'clock on the 22nd of Jan, 2020"
...
- objectB
...
这种方法让我可以轻松查询特定日期或月份并从中获取值。它还可以让我轻松清除特定日期或月份的旧数据。
但缺点是我想不出一种简单的方法来查询它以将我的应用程序中的数据作为按时间排序的平面列表(在大多数情况下,这就是我需要数据的方式)
2) 将数据存储为属性的平面日期列表
objects
- objectA
- 2020:01:22:01
- property1 : "Value at 1 o'clock on the 22nd of Jan, 2020"
- property2 : "Value at 1 o'clock on the 22nd of Jan, 2020"
- 2020:01:22:02
- property1 : "Value at 2 o'clock on the 22nd of Jan, 2020"
- property2 : "Value at 2 o'clock on the 22nd of Jan, 2020"
...
- objectB
...
此模型可以更轻松地以我需要它们在应用程序中的格式获取对象,但现在更难查询特定的日期或月份。而且清除特定日期或月份的旧数据并不容易。
在实践中这样做的好方法是什么? 我的任何一种方法都很好,还是我应该完全做其他事情? 如果一个比它好,我该如何解决我的缺点?
我的应用程序是用 Kotlin 编写的,那么在 kotlin 中是否有一种简单的方法可以从选项 1 中查询数据库并将其返回到平面列表的 for 中?
【问题讨论】:
标签: android firebase kotlin firebase-realtime-database