我有以下路径模式:
/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
上添加了一个新框,然后触发了“值”事件,我只是不明白为什么。
为什么 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())
})
最佳答案
关于ios - 离线时不会一直触发 Firebase 值事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42386692/