How to create a negative Firebase timestamp in fast

For the iOS app I'm working on, I need to receive messages in descending order. The first message comes first, and then the second new message, etc.

From looking at other answers and SO research, it seems like the best approach to my situation is to create a negative timestamp and then save this database as an additional property for messages.

Then I use queryOrderedByChild('negativeTimestamp') to retrieve the messages in observeSingleEvent , and then to see the messages sent after the initial calls, there will be a child of the Addded observer.

The firebase documentation says that I can get the server timestamp value from this code snippet firebase.database.ServerValue.TIMESTAMP

How to write this for Swift 3?

+5
source share
2 answers

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.

+2
source

Whether it is for Swift or not, another conceptual solution is to rely on the Firebase server time offset value.

This is not as accurate as firebase.database.ServerValue.TIMESTAMP , but the difference is usually in milliseconds. The advantage is that it allows you to create a negative timestamp on the client without updating the Firebase node twice.

You get the server time offset value when you need it from Firebase, generate a negative timestamp on the client, and then save your object in Firebase once.

See: https://groups.google.com/forum/#!topic/firebase-talk/EXMbZmyGWgE https://firebase.google.com/docs/database/ios/offline-capabilities#clock-skew (for iOS) . https://firebase.google.com/docs/database/web/offline-capabilities#clock-skew (for the web).

 var offsetRef = firebase.database().ref(".info/serverTimeOffset"); offsetRef.on("value", function(snap) { var offset = snap.val(); var negativeTimestamp = (new Date().getTime() + offset) * -1; }); 
+2
source

Source: https://habr.com/ru/post/1266297/


All Articles