Reputation: 1894
I'd like to update (or synchronize) the latest entry from a tree in my Firebase-Database.
The structure looks like this:
I want to observe the latest entry only, without fetching all entries as 'EventTypeChildAdded' does.
However, I really want to observe the latest entry, so I'd like to call a function whenever there is a new 'latest' entry, so when a child gets added.
I already found this piece of code.
(DatabaseRef).queryOrderedByKey().queryLimitedToLast(1).observeSingleEventOfType
But this does not seem to observe the latest entry.
Upvotes: 0
Views: 682
Reputation: 3654
I guess you are on the wrong way. childadded does exactly what you need. Check the doc again. https://firebase.google.com/docs/database/admin/retrieve-data#child-added
For the first time childadded fetches all the list but then only the item just added to the list.
Child Added
The child_added event is typically used when retrieving a list of items from the database. Unlike value which returns the entire contents of the location, child_added is triggered once for each existing child and then again every time a new child is added to the specified path. The event callback is passed a snapshot containing the new child's data. For ordering purposes, it is also passed a second argument containing the key of the previous child.
EDIT: To limit the childadded query to the last item:
ref.queryLimited(toLast: 1).observe(.childAdded) { (snapshot) in
// parse snapshot to get the last item
Upvotes: 1