我有一个大型数据集(约 100k 条目),正在使用“child_added”事件订阅该数据集。使用 Node 7 和 firebase 3.6.1,执行此操作似乎会在触发单个 child_added 事件之前下载整个 100k 条目。
内存消耗在几十秒内显着增长,然后所有 child_added 事件都会快速触发。
这很慢:
require('firebase').
initializeApp({databaseURL: 'https://someproject.firebaseio.com'}).
database().ref('data').
on('child_added', (snap) => console.log(snap.key));
限制仍然很快(几秒钟的延迟):
require('firebase').
initializeApp({databaseURL: 'https://someproject.firebaseio.com'}).
database().ref('data').limitToFirst(10).
on('child_added', (snap) => console.log(snap.key));
鉴于 Firebase 的流式传输性质,我认为 child_added 订阅在完成任何操作之前将整个数据集下载到客户端并不是预期的行为。
我做错了什么,还是这是一个错误?
最佳答案
尽管在child_added部分中提取自firebase documentation它说:
The child_added event is typically used when retrieving a list of items from the database. Unlike value which returns the entire contents of the location, child_added is triggered once for each existing child and then again every time a new child is added to the specified path. The event callback is passed a snapshot containing the new child's data. For ordering purposes, it is also passed a second argument containing the key of the previous child.
At the first lines在该页面中,我们可以找到:
Data stored in a Firebase Realtime Database is retrieved by attaching an asynchronous listener to a database reference. The listener will be triggered once for the initial state of the data and again anytime the data changes.
似乎是其正常行为。它首先检索所有数据。
关于javascript - Child_added 订阅似乎下载了整个数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40768589/