First, see the related answer in the comments. This response relies on the client to create a timestamp that made a negative and is written to Firebase.
If you want Firebase to generate a timestamp, you can do this with this small Firebase structure and code snippet.
First, consider the structure
root parent_node -Y8j8a8jsjd0adas time_stamp: -1492030228007 timestamp: 1492030228007
Next, some code to create and work with this structure:
Define a var that we can use in our class that references a Firebase timestamp
let kFirebaseServerValueTimestamp = [".sv":"timestamp"]
and a function that adds an observer to the node timestamp:
func attachObserver() { let timestampRef = self.ref.child("timestamp") let parentNodeRef = self.ref.child("parent_node") var count = 0 timestampRef.observe(.value, with: { snapshot in if snapshot.exists() { count += 1 if count > 1 { let ts = snapshot.value as! Int let neg_ts = -ts let childNodeRef = parentNodeRef.childByAutoId() let childRef = childNodeRef.child("time_stamp") childRef.setValue(neg_ts) count = 0 } } })
And a function that records the timestamp, whereby the observer fires, which creates the child nodes in parent_node based on the Firebase timestamp
func doTimestamp() { let timestampRef = self.ref.child("timestamp") timestampRef.setValue(kFirebaseServerValueTimestamp) }
Here is a summary.
In the attachObserver function, we attach an observer to the node timestamp - that the node may or may not exist, but if it is not created, read on. The code in the closure is called at any time when the event occurs in the node's timestamp.
When the doTimestamp function is called, it creates and writes the timestamp to the node's timestamp, which then starts the observer that we attach to attachObserver.
The code at the close of the observations does the following:
Make sure there is something in the picture, and if so, increase the counter (more on that a bit). If the counter is greater than 1, enter the timestamp as an integer from the snapshot. Then create it negative and write it back to Firebase as the parent of parent_node.
As it would be applicable at any time when you want to create a timestamp for a child node with a Firebase-generated timestamp, but a negative value for reverse loading / sorting - which tells the OP question.
The result is that when this happens,
timestampRef.setValue(kFirebaseServerValueTimestamp)
In fact, it writes twice to the node file, which will cause the code to be closer to the call twice.
Firebaser might explain this, but we need to ignore the first event and capture the second, which is the actual timestamp.
So, the first event will cause the observer to approach the fire by making count = 1, which will be ignored due to the if statement.
Then a second event occurs that contains the actual timestamp, and what we use to do negative and write to Firebase.
Hope this helps the OP and commentators.