1

I want to query active items from a Firebase node. An active item is delimited by a start date and an end.
Here is a sample of the structure :

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

Dates are Firebase timestamps to prevent user time shifting.
An item is saved on Firebase server like this:

//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])
    }
})

How can I query Firebase to get items where endDate >= FIRServerValue.timestamp() (active items) ?

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

adjuremods
  • 2,938
  • 2
  • 12
  • 17
Bogy
  • 944
  • 14
  • 30
  • 1
    Any reason you can't query on endDate startingAtValue(currentTimeStamp)? That will return all nodes where the endDate is greater than the currentTime. Maybe I don't fully understand the question? Do you always want to write the current time stamp as the start date and the end date will always be one hour later? – Jay Oct 26 '16 at 18:25
  • I can query `endDate startingAtValue(currentTimeStamp)` but to get currentTimeStamp I need to save `FIRServerValue.timestamp()` a first time on firebase to get the current value. It would be a resources-consuming query so maybe there is another solution to deal with timestamps. – Bogy Oct 27 '16 at 11:53
  • And yes I always want to save the current times tamp as the start date and the end date can vary (1 hour or more). – Bogy Oct 27 '16 at 11:55

1 Answers1

1

You could try something like this. This would only query items where the end date equals the current date or greater.

let removeBeforeThisDate = NSDate()
let currentDateTimestamp = removeBeforeThisDate.timeIntervalSince1970

itemRef.queryOrderedByChild("endDate").queryStartingAtValue(currentDateTimestamp).observeEventType(.Value, withBlock: { snapshot in
...
chickenparm
  • 1,570
  • 1
  • 16
  • 36
  • I agree but the timestamp can be easily modified by the user if he changes the time of his device. – Bogy Oct 27 '16 at 12:01
  • You can use let serverTime = ServerValue.timestamp() which you can pass as a value to a query. The server will interpret this as the current time stamp when it is received. Look here for more information: https://stackoverflow.com/questions/25536547/how-do-i-get-a-server-timestamp-from-firebases-ios-api – DoesData Jan 04 '18 at 13:47