简体   繁体   中英

How to update latest entry from Firebase-Database tree?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM