Search code examples
swiftfirebasefirebase-realtime-databaseswift4observers

How to use wildcards in a firebase observer path ?


Is it possible to use wildcards in the path of a firebase observer ?

Right now I have :

self.ref?.child('root').child('info').observe(
    DataEventType.childChanged, with: { (snapshot) -> Void in

    //do something   

})

Is there any way to achieve something like :

self.ref?.child('root').child('info').child(<wildcard>).child('details').observe(
     DataEventType.childChanged, with: { (snapshot) -> Void in

    //find out what the wildcard is & do something

})

Thanks


Solution

  • There is no way to observe a subset of nodes like that. Your options are:

    1. Observe all child nodes of info, which childAdded, etc.
    2. Move the details to a new top-level node and observe on there.

    The first option works with your current data structure. But it will read more data than you're interested in. The second option doesn't download too much data, but you'll need to change your data structure.