简体   繁体   English

如何更新Firebase-Database树中的最新条目?

[英]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. 我想从Firebase数据库中的树中更新(或同步)最新条目。

The structure looks like this: 结构如下:

I want to observe the latest entry only, without fetching all entries as 'EventTypeChildAdded' does. 我只想观察最新的条目,而不像'EventTypeChildAdded'那样获取所有条目。
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. childadded确实满足您的需求。 Check the doc again. 再次检查文档。 https://firebase.google.com/docs/database/admin/retrieve-data#child-added 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. childadded第一次获取所有列表,但仅是刚刚添加到列表中的项目。

Child Added 子级已添加

The child_added event is typically used when retrieving a list of items from the database. 通常从数据库中检索项目列表时使用child_added事件。 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. 与返回位置的全部内容的value不同,对每个现有子项都触发一次child_added,然后在每次将新子项添加到指定路径时再次触发。 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: 编辑:限制childadd查询到最后一项:

ref.queryLimited(toLast: 1).observe(.childAdded) { (snapshot) in
// parse snapshot to get the last item

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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