Kiow
Kiow

Reputation: 880

Snapshot returned from Firebase database event childChanged on iOS

In my app I fetch data from Firebase when a node is changed:

handle = ref?.child("orders").child("4").observe(.childChanged, with: { (snapshot) in          
    if snapshot.key == "price" {
        print("price: \(snapshot.value)")
    }
    if snapshot.key == "orders" {
        print("orders: \(snapshot.value)")
    }                                 
}, withCancel: nil)

But is the above function really the way to do it?

I have tried to setup a if statement to check what values is being return then grab that value from snapshot.value but I am worried if I change all my values at once I can't tell from what node snapshot.value is coming from.

Upvotes: 1

Views: 1374

Answers (1)

Paulo Mattos
Paulo Mattos

Reputation: 19339

Yes, for tracking children nodes changes that's the way to do it!

Firebase documentation, for the corresponding childChanges database event, provides further details:

Listen for changes to the items in a list. This event is triggered any time a child node is modified. This includes any modifications to descendants of the child node. The FIRDataSnapshot passed to the event listener contains the updated data for the child.

As such, snapshot.key identifies the updated child node and snapshot.value the corresponding data.

Upvotes: 2

Related Questions