eurosecom
eurosecom

Reputation: 2992

Android Firebase negative ServerValue.TIMESTAMP for ordering

I use ServerValue.TIMESTAMP in Firebase for ordering in query. I set timestamp in my model class to dats for example 1494325636954.

enter image description here

public Attendance(String usico) {
    this.usico = usico;

    HashMap<String, Object> datsObj = new HashMap<String, Object>();
    datsObj.put("date", ServerValue.TIMESTAMP);
    this.dats = datsObj;

}

How can I set to datt negative ServerValue.TIMESTAMP for example -1494325636954 ?

Upvotes: 1

Views: 778

Answers (2)

user6597752
user6597752

Reputation: 190

Use timeStamp in other cases but when it comes to firebase you don't need to go all the to convert timeStamp to negative for just sorting, my way is put decrementing long or int whenever child is added (inside child's property) then use that as a sorting method, this will simplify lot of time if you care only about order .

Upvotes: 0

Bradley Mackey
Bradley Mackey

Reputation: 7668

You can create a Firebase Function to update the value to be negative, while maintaining the server accurate timestamp.

exports.makeNegativeTimestamp = functions.database.ref('/posts/{postID}/date').onWrite(event => {
    // get the timestamp from the DeltaSnapshot
    const timestamp = event.data.val();
    // ensure that the timestamp is a number
    if (isNaN(timestamp)) { return; }
    // only make negative if it's currently positive
    if (timestamp >= 0) {
        // returns a promise
        return event.data.adminRef.set(timestamp * -1);
    }
});

Upvotes: 1

Related Questions