Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a server timestamp from Firebase's iOS API?

I have an iOS app that uses Firebase and currently has a few dictionaries with keys that are NSDate objects. The obvious issue with this is that NSDate draws from the device's system time, which is not universal.

With that, what's the best way to get a server timestamp (similar to Firebase.ServerValue.TIMESTAMP for the Web API) using Firebase's iOS API so that I can sort my dictionary keys chronologically?

I'm also aware of the chronological nature of IDs generated by childByAutoID, but I can't figure out the proper way to sort these in code. While they may be returned in chronological order, any time something like allKeys is called on them, the order goes out the window.

Any help with this issue would be greatly appreciated!

like image 605
Alex Costantini Avatar asked Aug 27 '14 20:08

Alex Costantini


People also ask

How to get Firebase server time in Swift?

Update: In Firebase 3.0 + Swift, you can use FIRServerValue. timestamp() . In Objective-C this is [FIRServerValue timestamp] . In Swift, you can now use FirebaseServerValue.

What does firebase timestamp return?

What is timestamp in firebase? A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one.

What is the format of firestore timestamp?

Date and time in the Firestore are saved in their proprietary date format — called Timestamp. Firestore timestamp is just a simple object with two properties — seconds and nanoseconds, which apparently has some advantages over “classic” date format.


1 Answers

Update: In Firebase 3.0 + Swift, you can use FIRServerValue.timestamp(). In Objective-C this is [FIRServerValue timestamp].

In Swift, you can now use FirebaseServerValue.timestamp() with Firebase 2.0.3+ (before 3.0).

The equivalent for Firebase.ServerValue.TIMESTAMP in iOS is kFirebaseServerValueTimestamp. Right now, this only works for Objective-C and not Swift.

In Swift, you can create your own global timestamp with

let kFirebaseServerValueTimestamp = [".sv":"timestamp"]

and then you'll be able to use kFirebaseServerValueTimestamp in the same way.

But you can only use this as the value or priority of a node. You won't be able to set it as the key name (although, I don't believe you could in the Web API either).

In general, calling allKeys on a dictionary does not guarantee order. But if you're using childByAutoID at a node, you can get back the right order by ordering the NSArray returned by allKeys lexicographically. Something like this would work:

[ref observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
    NSDictionary *value = snapshot.value;
    NSLog(@"Unsorted allKeys: %@", value.allKeys);
    NSArray *sortedAllKeys = [value.allKeys sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"Sorted allKeys: %@", sortedArray);
}];

This is similar to sorting an NSArray alphabetically, but when sorting the auto-generated IDs, you do not want localized or case insensitive sort, so you use compare: instead of localizedCaseInsensitiveCompare:

like image 95
katfang Avatar answered Oct 17 '22 07:10

katfang