gooberboobbutt gooberboobbutt - 1 year ago 197
Swift Question

Can someone clearly explain difference between .Value, .ChildAdded, .ChildChanged, .ChildRemoved for FIRDataEventType?

I'm having trouble putting it into words. Can someone explain what the difference between the different FIRDataEventTypes and examples of when it would be used?

Example (SWIFT):

let queryRef = FIRDatabase.database().reference().child("user")
queryRef.observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in


queryRef.observeEventType(.Value, withBlock: { (snapshot) -> Void in

From testing, .Value returns one object while .ChildAdded returns multiple; When doing advanced queries .ChildAdded doesn't work but .Value somewhat works (deeper children are null).

Answer Source

tl;dr - Watch this video. It uses the old SDK in Android, but the idea is the exact same even for iOS.

Each one of these events is a specific way to handle synchronization of data across clients.

The Value event will fire each time any piece of data is updated. This could be a newly added key, a deletion of a key, or an update of any value at the reference. When the change happens the SDK sends back the entire state of the object, not the delta just change that occurred.

The Child added event will fire off once per existing piece of data, the snapshot value will be an individual record rather than the entire list like you would get with the value event. As more items come in, this event will fire off with each item.

The Child removed and changed events work almost the same. When an item is deleted or has it's value changed, the individual item is returned in the callback.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download