将firebase数据有效地汇总起来 [英] Sum firebase data efficiently up

查看:126
本文介绍了将firebase数据有效地汇总起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,在火力点表中有几千个条目。
现在有人连接到这个表,我需要统计和总结这些项目。

  -itemlist 
|
- 1
| -x:2
- 2
| -x:4

等等。用户可以随时连接(也就是说他以前从来没有见过这个表),另外一个用户可以在任何时候改变某个值或者增加值。



现在,如果我尝试添加一个 ValueEventListener 到itemlist,它将起作用,如果没有新的数据,一旦我连接到1000个条目左右的新数据库,监听器就会调用这1000个条目中的每一个。

我尝试过使用onChildlistener,但是这次调用了第一次添加的每一项额外的ChildItem。
有时需要2分钟。



有没有办法让整个数据库一次,计算出来,然后只听更改?

解决方案

Firebase数据库没有服务器端的聚合操作符,因为它们本质上与数据库的可伸缩性和实时特性不符。


$ b


  1. 按照您的建议在客户端执行它们


  2. 保存每次更改后更新的运行汇总 >如果客户端已经需要显示数据,那么执行聚合客户端是一个不错的选择。例如:如果您显示的是特定聊天室中的用户列表,则可以很容易地从相同的数据中统计客户端的用户数。



    但是如果你不需要数据客户端,那么只要下载它来聚合它是浪费,将会伤害你的应用程序的可扩展性。在这种情况下,一个选项是保留一个额外的节点来保持聚合,并用每个相关的写操作来更新它。例如:如果您想显示有多少用户在您的应用程序中进行了注册,那么您可以保留每当用户注册/取消注册时更新的全局 / user_count 。对于此次更新,您通常会使用交易。 / p>

    I have an app where I have up to a few thousand entries in a firebase table. Now someone is connecting to this table and I need to count and sum these items up.

    -itemlist
    |
    --1
      |-x:2
    --2
      |-x:4
    

    and so on. A user can connect at any time (meaning he never saw this table before) and another user can change some value at any given time or add values.

    Now if I try and add a ValueEventListener to "itemlist" it works if there is no new data but as soon as I connect to a new database with 1000 entries or so the listener calls for every one of these 1000 entries.

    I tried with onChildlistener but this calls the on ChildItem added for every item extra on the first time. This takes over 2 minutes sometimes.

    Is there a way to get the whole database once, calculate it and then only listen for changes?

    解决方案

    The Firebase Database has no server-side aggregation operators, since they would inherently be at odds with the scalability and realtime nature of the database.

    That leaves you with two options to calculate aggregates:

    1. perform them client side as you already suggested

    2. keep a running aggregate that you update with every change

    Performing the aggregation client-side is a good option if the client already needs to display the data anyway. For example: if you're showing the list of users in a specific chat room, you can easily count the number of users client-side from the same data.

    But if you don't need the data client-side, then just downloading it to aggregate it is wasteful and will hurt scalability of your app. In such cases, an option is to keep an extra node that keeps the aggregate and update it with every relevant write operation. For example: if you want to show how many users have registered with your app, you could keep a global /user_count that you update whenever a user registers/unregisters. For this update, you'd typically use a transaction.

    这篇关于将firebase数据有效地汇总起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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