How to create negative Firebase timestamp in swift

前端 未结 2 492
长情又很酷
长情又很酷 2020-12-07 01:54

For an iOS app I am working on, I need to fetch messages in descending order i.e the latest message comes first, followed by second newest message etc.

From looking

2条回答
  •  甜味超标
    2020-12-07 02:37

    Regardless whether it's for Swift or not, another conceptual solution is to rely on Firebase's server time offset value.

    It's not as precise as firebase.database.ServerValue.TIMESTAMP, but the difference is usually within milliseconds. The advantage is that it lets you create a negative timestamp on the client without having to update your Firebase node twice.

    You grab the server time offset value when you need it from Firebase, generate the 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 web).

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

提交回复
热议问题