如何从Firebase的iOS API获取服务器时间戳? [英] How do I get a server timestamp from Firebase's iOS API?

查看:201
本文介绍了如何从Firebase的iOS API获取服务器时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Firebase的iOS应用程序,目前有几个字典的键是NSDate对象。与此明显的问题是,NSDate从设备的系统时间,这是不通用的。



有了这个,获得服务器时间戳的最佳方式是什么Firebase.ServerValue.TIMESTAMP为Web API)使用Firebase的iOS API,以便我可以按时间顺序排序我的字典键?



我也知道ID的年代性质由childByAutoID生成,但我无法弄清楚在代码中排序这些东西的正确方法。虽然他们可能会按照时间顺序返回,但任何时候都可以调用类似于所有键的命令,命令会跳出窗口。



有关此问题的任何帮助将不胜感激! 在Firebase 3.0 + Swift中,您可以使用
FIRServerValue.timestamp()。在Objective-C中,这是 [FIRServerValue timestamp]



在Swift中,现在可以使用<$ c $ FirebaseServerValue.timestamp()与Firebase 2.0.3+(3.0之前)。



Firebase .ServerValue.TIMESTAMP 在iOS中是 kFirebaseServerValueTimestamp 。现在,这只适用于Objective-C而不是Swift。



在Swift中,您可以创建您自己的全球时间戳记

 让kFirebaseServerValueTimestamp = [.sv:timestamp] 

然后你就可以以同样的方式使用 kFirebaseServerValueTimestamp

但是,您只能将此用作节点的值或优先级。您将无法将其设置为关键名称(尽管我不相信您也可以在Web API中)。



通常,调用 allKeys 在字典上 不保证 但是如果你在一个节点上使用 childByAutoID ,你可以通过订购由 allKeys返回的NSArray返回正确的顺序按字典顺序。类似这样的工作:
$ b $ pre $ [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);
}];

这与排序 NSArray按字母顺序排列,但排序自动生成的ID时,您不需要 使用比较:而不是 localizedCaseInsensitiveCompare:


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!

解决方案

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:

这篇关于如何从Firebase的iOS API获取服务器时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆