简体   繁体   中英

Firebase Realtime Database, how do I know when dataset has finished loading

How do I know when the dataset is loaded?

I have been using Firebase for over five years and love it. But, I have one question which I hope you can help with, but has been driving me mad for years.

I have many datasets stored on Firebase. What I need is a way to know when the dataset has been loaded. So, if there are 1000 records at the node /Main/PNG/flight/airstrip I want to know when I have received all leaves.

I have tried using .value and subscribe to the whole node using

Database.database().reference(withPath: key).observe(.value) { [weak self, key] snapshot ...}

And then I will know when the dataset comes in because I will get just one snapshot containing a dictionary of all the leaves, but I don't get the deltas (changes and additions).

and I have subscribed to all the leaves, using

[.childChanged, .childRemoved, .childAdded].forEach { eventType in
Database.database().reference(withPath: key).observe(eventType) { [weak self, storeEventType] snapshot in ...}

But, then I don't know when I have received all of the dataset.

My datamodel is based on an Initial Snapshot followed by listening to any changes to the leaves. The initial snapshot is easy using

Database.database().reference(withPath: key).observe(.value) { [weak self, key] snapshot ...}

but when I follow this with

[.childChanged, .childRemoved, .childAdded].forEach { eventType in
Database.database().reference(withPath: key).observe(eventType) { [weak self, storeEventType] snapshot in ...}

I get the complete dataset again.

What I would like is either :

  1. to .observe(.childAdded) and somehow know when all the leaves have completed, at least for this dataset,
  2. or to use .observe(.childAdded) without receiving an initial dataset of all the leaves, but then I need to work out if I missed anything between the .observe(.value) and .observe(.childAdded) calls.

I know that I could view the dataset as an endless number of deltas and changes, so therefore there isn't an END OF SET , but updating a UITableView in Swift is easier if I know lots of records are queued, in which case I can call a reloadData() on the UITableview. But, if there are just a few records queued, I prefer to let the tableView animate nicely, a row at a time.

When an entire snapshot is loaded, the Firebase SDK fires the value event. So even if you use the child* events for actually handling the data, you can implement a value listener to detect when the loading is done.

Implementing both child* and value event listeners does not double the amount of data that gets load. The Firebase SDK deduplicates the listeners behind the scenes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM