1

Before you link me to a duplicate, please read what I'm asking..

I'm building an app which basically has a list of about 5000 teams. These teams are fairly static (they don't change very often). I would like to observe any time one is changed though as it's essential it get's updated in the app ASAP.

If I include dbTeams.ref.observe(.childAdded, with: {}), it runs each time the app starts, loading over all 5000 records despite having them in the persistent storage already (I have enabled persistence).

Now the documentation says this will happen, I know, but with 5000 records (and potentially way more in the future), I can't have this happen.

My options so far (from what I've found and tried) are:

  1. Add a timestamp to each record and create a custom query to call .childAdded after the last timestamp... This is inefficient. Storing a timestamp for soccer teams which will hardly ever change, is silly. It also means keeping a copy of the last time it was checked.
  2. Create a sub-list within the Teams list. This too is silly as you may as well call .value and get the whole bunch of data in one go.
  3. Just live with it... Fine - until it scales to tens of thousands of records. Not clever either.

It just seems weird that all the other event listeners only fire when they are "supposed to" except this one.

Any help would be appreciated - how do I achieve what I need?

AL.
  • 36,815
  • 10
  • 142
  • 281
Byron Coetsee
  • 3,533
  • 5
  • 20
  • 31
  • The .childAdded event does fire when it's supposed to. It's supposed to fire initially once for each child node and any time a child node is added thereafter. It's not clear what your expected result is and honestly 5000 records may not be a whole lot of data depending on your structure (which should also be included in the question). Obviously 100k is probably too much more but that's defined by a query; for example query(startingAt: tenDaysAgo).queryEndingAt(today) would only load child notes from 10 days ago to today. – Jay Apr 10 '17 at 18:39
  • Thanks for the response @Jay. I would like to observe any time a new team is added but I don't want to run through every team each time the app starts. Like you suggested and like I said in my question, time based queries are fine but I have no way of checking how far back to query... – Byron Coetsee Apr 11 '17 at 12:21
  • What defines when something is new and when it isn't? If a team is added yesterday, is that a new team or old because it was yesterday. When you start the app, you don't want to load any teams at all, just display teams that are added from that time going forward? – Jay Apr 12 '17 at 18:26
  • @Jay a new team is one that is not currently stored on the local (persistent) firebase db... So a team could be added a week ago, but the app was run 2 weeks ago and therefor doesn't have the new team synced. So I'd have preferred the .childAdded to be run only once - for that new team. – Byron Coetsee Apr 13 '17 at 08:14
  • I don't think that's a good use case for Firebase. While it offers offline persistence (for disconnects etc), it's really an online, live, event driven NoSQL database. By trying to keep some things offline, and some things online I don't think it will work as you expect and it's going to be a nightmare to maintain. It seem you may already running into that with this scenario. However, I could be totally wrong... maybe a Firebaser could provide some insight and thoughts on the matter. – Jay Apr 13 '17 at 21:43
  • Take a look and this question and answer as well. Not totally on topic but adds some insight from a Firebaser. [Firebase offline capabilities as cache](http://stackoverflow.com/questions/38778680/firebase-offline-capabilities-as-cache/38790754#38790754) – Jay Apr 14 '17 at 21:02

0 Answers0