【发布时间】:2017-07-12 05:15:37
【问题描述】:
我有以下路径模式:
/ID_Company/boxes/timestamp_of_the_day/ID_box
假设我刚刚开始新的一天,但我处于离线状态。目前在 Firebase DB 上,路径 /ID_Company/boxes/timestamp_of_TODAY 不存在,缓存中也不存在。
不,我在路径 /ID_Company/boxes/timestamp_of_TODAY/id_box1 中添加了一个新框
如果我有childAdded 事件的观察者,它将被触发。但是如果我在value 事件上有观察者,则不会触发任何事情。
现在假设我在添加第一个框时在线。所以在firebase上这条路径/ID_Company/boxes/timestamp_of_TODAY/id_box1存在,所以它在本地存在。
它离线了。我在/ID_Company/boxes/timestamp_of_TODAY/id_box2 上添加了一个新框,然后触发了 'value` 事件,我就是不明白为什么。
为什么timestamp_of_TODAY已经存在时触发,不存在时不触发?
感谢您的帮助。
编辑:
这是我添加框的方法:
guard let startingTimestamp = date.beginning(of: .day)?.timeIntervalSince1970 else { return nil }
let boxRef = dbRef.child("ID_Company").child("boxes").child("\(startingTimestamp)").childByAutoId()
var box = box
box.id = boxRef.key
boxRef.setValue(box.toDictionary()) { error, ref in
if let error = error as? NSError {
print(error)
completion(error)
} else {
completion(nil)
}
}
这是我获得盒子的方法:
guard let startingTimestamp = day.beginning(of: .day)?.timeIntervalSince1970, let endingTimestamp = day.end(of: .day)?.timeIntervalSince1970 else { return nil }
let boxesRef = dbRef.child("ID_Company").child("boxes").child("\(startingTimestamp)")
let query = boxesRef.queryOrdered(byChild: Box.Key.dateTimestamp.rawValue).queryStarting(atValue: startingTimestamp).queryEnding(atValue: endingTimestamp + 0.001)
let handle = query.observe(.value, with: { snapshot in
var boxes: [Box] = []
for child in snapshot.children {
let box = Box(snapshot: child as! FIRDataSnapshot)
if userID == nil || box.userID == userID! {
boxes.append(box)
}
}
completion(boxes.reversed())
})
【问题讨论】:
-
一些代码怎么样?你能用用于将观察者附加到节点的代码更新你的问题吗?
-
@Jay 我刚刚编辑了问题
-
我想从你的问题中理解这句话。 “如果我在 childAdded 事件上有一个观察者,它将被触发。但如果我在 value 事件上有一个观察者,则不会触发任何事情。” .在哪条路径上有调用的 childAdded 事件,在哪条路径上有没有调用的 value 事件?
标签: ios swift firebase firebase-realtime-database