Bbx
Bbx

Reputation: 3384

In Firebase Database, how to read a large list then get child updates, efficiently

I want to load a large list 5000 items, say, from a Firebase Database as fast as possible and then get any updates when a child is added or changed.

My approach so far is use ObserveSingleEvent to load the entire list at startup

pathRef.ObserveSingleEvent(DataEventType.Value, (snapshot) => 

and when that has finished use ObserveEvent for child changes and additions:

nuint observerHandle1 = pathRef.ObserveEvent(DataEventType.ChildChanged, (snapshot) =>

nuint observerHandle2 = pathRef.ObserveEvent(DataEventType.ChildAdded, (snapshot) =>

The problem is the ChildAdded event is triggered 5000 times, all of which are usually unnecessary and it takes a long time to complete. (From the docs: "child_added is triggered once for each existing child and then again every time a new child is added to the specified path)

And I can't just ignore the first update for each child, because it might be a genuine change (eg database updated just after the ObserveSingleEvent completed). So I have to check each item on the client and see if it has changed. This is all very slow and inefficient.

I've seen posts suggesting to use a query with LimitToLast etc, but this is not reliable as child changes may be missed.

My Plan B is to not do ObserveSingleEvent to load the list and just rely on the 5000 child added calls, but that seems crazy and is slower to get the list initially, and probably uses more of the user's data allowance.

I would think this is a common scenario, so what is the best solution?

Also, I have persistence enabled, so a couple of related questions:

1) If the entire list has been loaded before, does ObserveSingleEvent with Value just load from disk and there's no network use, when online (but no changes have happened since last download) or does it download the whole list again?

2) Similarly with the 5000 calls to child added - if it's all on disk, is there any network use, when online if no changes?

(I'm using C# for iOS, but I'm sure its the same issue for other platforms)

Upvotes: 0

Views: 544

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598887

The wire traffic for a value listener and for a child_ listener is exactly the same. The events are purely a client-side interpretation of the underlying data.

If you need all children, consider using only the child_* listeners and no pathRef.ObserveSingleEvent(DataEventType.Value. It'll lead to the simplest code.

Upvotes: 0

Related Questions