【发布时间】:2017-03-09 04:55:34
【问题描述】:
我想从 Firebase 节点查询活动项目。活动项目由开始日期和结束日期分隔。
这是一个结构示例:
{
"items" : {
"itme1" : {
"data" : "...", //some data
"startDate" : 12345678,
"endDate" : 23456789
},
"item2" : {...}
}
}
日期是 Firebase 时间戳,以防止用户时移。
一个项目像这样保存在 Firebase 服务器上:
//Init data to save
var dictionary = Dictionary<String,Any>()
dictionary["data"] = "some data"
dictionary["startDate"] = FIRServerValue.timestamp()
//Save data on Firebase
let itemRef = FIRDatabase.database().reference(withPath: "items").childByAutoId()
itemRef.setValue(dictionary)
//Set end date
itemRef.observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in
if let dict = snapshot.value as? NSDictionary, let createdAt = dict["startDate"] as? TimeInterval {
let startDate = Date(firebaseTimestamp: createdAt)
let endDate = startDate.dateByAddingHours(1) //Add one hour to startDate
itemRef.updateChildValues(["endDate":endDate.timeIntervalSince1970 * 1000])
}
})
如何查询 Firebase 以获取 endDate >= FIRServerValue.timestamp()(活动项目)所在的项目?
由于firebase时间戳FIRServerValue.timestamp()只有在Firebase上写入后才能知道,我无法直接查询FIRServerValue.timestamp()。
也许有更好的方法来设置 endDate。人们如何处理这个问题?
【问题讨论】:
-
有什么原因不能查询 endDate startingAtValue(currentTimeStamp)?这将返回 endDate 大于 currentTime 的所有节点。也许我不完全理解这个问题?您是否总是想将当前时间戳记写为开始日期,而结束日期总是晚一小时?
-
我可以查询
endDate startingAtValue(currentTimeStamp)但要获得 currentTimeStamp 我需要第一次在 firebase 上保存FIRServerValue.timestamp()以获得当前值。这将是一个消耗资源的查询,所以也许有另一种解决方案来处理时间戳。 -
是的,我总是想保存当前时间戳,因为开始日期和结束日期可能会有所不同(1 小时或更长时间)。
标签: swift firebase firebase-realtime-database