Firebase observe with type .childAdded retrieves all my information every time. Please assist

人盡茶涼 提交于 2019-12-29 05:37:25

问题


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 :/


回答1:


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


来源:https://stackoverflow.com/questions/39584091/firebase-observe-with-type-childadded-retrieves-all-my-information-every-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!