Jack Guo
Jack Guo

Reputation: 4664

Firebase Realtime Database listen to .childChanged at specific child path

Is it possible to get notified when a sub child of a child changed? For instance

posts
    post1
        text: ...
        comments: ...
        likes: 2
    post2
        text: ...
        comments: ...
        likes: 30

I want to get a post whenever its likes changed. I tried to use root.child("posts").queryOrdered(byChild: "likes").observe(.childChanged) but it doesn't work

Upvotes: 0

Views: 243

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

A Firebase query listens to the data directly under the location where you attach the query. A child... listener fires for the child node immediately under the location where you attach the query.

There is no way to listen to only a specific property of each child. If you need that, you will have to create a separate branch in your JSON for just those properties. E.g.

posts
    post1
        text: ...
        comments: ...
        likes: 2
    post2
        text: ...
        comments: ...
        likes: 30
likeCounts
    post1: 2
    post2: 30

Now you can listen to just /likeCounts for the changes you want.

Upvotes: 1

Related Questions