Ignacio Oroná
Ignacio Oroná

Reputation: 4399

Firebase doesn't call back when using '.childAdded'

I have the following code, written in Swift which I expect to be called each time a new record has been added to the my Database:

var databaseRef = Firebase()
    databaseRef = Firebase.init(url: "<MY_PROJECT_URL>")
    databaseRef.child(byAppendingPath: "channels").queryLimited(toFirst: 100).observe(.childAdded , with: { (snapshot) in
          print("New Message input by user")
    })

And this is my data structure:

enter image description here

So I basically create a listener for the branch 'channels'. The completion handler gets called only at the start of my program, then, never again. The strange thing is that if I use '.value' rather than '.childAdded' it does work! I am not interested in using '.value' since that returns me the whole chunk of data inside the 'channels' branch and I am really interested only in the single record that was added. (actually one of those L1... guys. A new one of course) Any ideas?

Upvotes: 2

Views: 121

Answers (1)

creeperspeak
creeperspeak

Reputation: 5523

If you are observing messages from all users at the same time I'd recommend restructuring your Firebase data a bit. Make messages a top-level object, and then you can just observe that whole section. I don't know what your messages look like, but I assume if you do this you will need to add a reference to the user inside the message so that you can get the new message added and still see which user wrote it.

Upvotes: 1

Related Questions