我想从Firebase节点查询活动项目。活动项目由开始日期和结束分隔。
下面是结构的一个示例:如何在Firebase上查询包含时间戳的项目?
{
"items" : {
"itme1" : {
"data" : "...", //some data
"startDate" : 12345678,
"endDate" : 23456789
},
"item2" : {...}
}
}
日期是火力地堡时间戳,以防止用户时移。
//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])
}
})
我如何查询火力地堡获得的物品,其中endDate >= FIRServerValue.timestamp()
(主动项):
一个项目这样就节省火力地堡的服务器上?
由于Firebase时间戳FIRServerValue.timestamp()
只能在Firebase上写入后才能知道,因此我无法直接查询FIRServerValue.timestamp()
。 也许有更好的方法来设置endDate。人们如何处理这个问题?
你无法在endDate上查询的原因startingAtValue(currentTimeStamp)?这将返回endDate大于当前时间的所有节点。也许我不完全理解这个问题?您是否总是希望将当前时间戳写入开始日期,并且结束日期总是在一小时后? – Jay
我可以查询'endDate startingAtValue(currentTimeStamp)',但为了得到currentTimeStamp我需要在Firebase上第一次保存'FIRServerValue.timestamp()'以获得当前值。这将是一个耗费资源的查询,所以也许有另一种解决时间戳的方法。 – Bogy
是的,我总是想保存当前时间戳,因为开始日期和结束日期可能会有所不同(1小时或更长时间)。 – Bogy