问题描述
我有一个查询(快速编写):
I have a query (written in swift):
FIRDatabase.database().reference(withPath: "\(ORDERS_PATH)/\(lId)")
.child("orders")
.observe(.childAdded, with: { firebaseSnapshot in
let orderObject = firebaseSnapshot.value as! [String: AnyObject]
let order = AppState.Order(
title: orderObject["name"] as! String,
subtitle: orderObject["item_variation_name"] as! String,
createdAt: Date(timeIntervalSince1970: TimeInterval(orderObject["created_at"] as! Int / 1000)),
name: "name",
status: AppState.Order.Status.Pending
)
f(order)
})
我的数据库如下:
我希望它只监听所有新收到的订单.但是,每次初次加载时,它都会获取一堆现有订单,这不是我想要的.
I want it to just listen all NEW incoming orders. However, every time it initially loads it fetched a bunch of existing orders with it, which isn't what I want.
我在每个订单上确实都有一个created_at
(代表该时间的int,例如1478637444000),因此,如果有可以利用的解决方案也可以使用.
I do have a created_at
(an int that represents that time e.g. 1478637444000) on each of the orders, so if there's a solution that can utilize that that works too.
我的查询出问题了吗?
推荐答案
观察者总是触发一次并读入所有数据".
Observers always fire once and read in "all the data".
.value观察者一次读取节点中的所有内容,其中作为.childAdded观察者(来自文档)
A .value observer reads in everything in the node at once, where as a .childAdded observer (from the docs)
如果您考虑自己的问题,那么您实际要查找的是所有数据的子集.这是通过查询实现的.
If you think about your question, what you are actually looking for is a sub-set of all of your data. This is achieved via a query.
您的查询将观察在某个时间戳记之后发生的所有.childAdded事件.这就是您需要做的!
Your query will be to observe all .childAdded events that occur after a certain timestamp. So that's what you need to do!
利用queryStartingAtValue(开始的时间戳)创建观察查询,该查询将返回在该时间戳之后添加的所有子项.
Craft an observe query leveraging queryStartingAtValue(the timestamp to start at) which will return all children added after that timestamp.
这篇关于Firebase:观察childAdded返回现有/旧记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!