Firebase观察类型.childAdded每次检索我的所有信息。请协助 [英] Firebase observe with type .childAdded retrieves all my information every time. Please assist
问题描述
每个现有的孩子触发一次ChildAdded,然后每次将新的孩子添加到指定的路径
所以,我有一个应用程序,顶部有一个小横幅,每当用户得到一个新的信息。您可能已经猜到了,这些消息存储在Firebase中的用户对象的子项中。所以,这是问题,当我加载应用程序,它弹出每个用户的消息。只有在添加新的孩子时才可以调用这个观察事件吗?我不希望每个现有的孩子都被触发,只有当新的孩子被添加。我不希望在核心数据中存储消息引用,并检查每个孩子是否已经存在于核心数据中:/ $ / $>
使用
。请参阅 Firebase,只接收新生儿。
ref.queryLimitedToLast(1)
queryOrderByKey()
.queryStartingAtValue(latestKey)
According to Firebase docs:
ChildAdded is triggered once for each existing child and then again every time a new child is added to the specified path
So, I have an app, that has a little banner at the top that pops up every time a user gets a new message. As you could have guessed, these messages are stored in a child in the user object in Firebase. So, here's the problem, when I load the app, it pops up for EVERY message the user has. Is it possible to have this observe event ONLY be called when a new child is added? I don't want it to be triggered for every single existing child, only when a new one is added. I would hate to have to store message references in core data, and do a check for every child to see if it already exists in core data :/
A few ways to do this:
- Use
ref.queryLimitedToLast(1)
when you start. See Firebase, only get new children. - keep track of the most recent key you've got in your local storage and then query with
queryOrderByKey()
.queryStartingAtValue(latestKey)
- Add a timestamp to the items and then
queryOrderByChild("timestamp")
.queryStartingAt(now)
for items with timestamp >= now. See how to discard initial data in a Firebase DB
这篇关于Firebase观察类型.childAdded每次检索我的所有信息。请协助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!