How to query items with timestamps on Firebase?
<p>I want to query active items from a Firebase node. An active item is delimited by a start date and an end.<br>
Here is a sample of the structure :</p>

<pre><code>{
    "items" : {
        "itme1" : {
            "data" : "...", //some data
            "startDate" : 12345678,
            "endDate" : 23456789
        },
        "item2" : {...}
    }
} 
</code></pre>

<p>Dates are Firebase timestamps to prevent user time shifting.<br>
An item is saved on Firebase server like this:</p>

<pre><code>//Init data to save
var dictionary = Dictionary&lt;String,Any&gt;()
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])
    }
})
</code></pre>

<p>How can I query Firebase to get items where <code>endDate &gt;= FIRServerValue.timestamp()</code> (active items) ?</p>

<p>As firebase timestamp <code>FIRServerValue.timestamp()</code> can only be known only after it has been written on Firebase, I cannot query <code>FIRServerValue.timestamp()</code> directly.
Maybe there is a better way to set endDate. How do people handle this?</p>