我有一个使用 Firebase 的 iOS 应用,目前有一些字典,其中的键是 NSDate 对象.这样做的一个明显问题是 NSDate 从设备的系统时间中提取,这不是通用的.
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.
这样,使用 Firebase 的 iOS API 获取服务器时间戳(类似于 Web API 的 Firebase.ServerValue.TIMESTAMP)以便我可以按时间顺序对字典键进行排序的最佳方法是什么?
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?
我也知道 childByAutoID 生成的 ID 的时间顺序,但我无法找出在代码中对它们进行排序的正确方法.虽然它们可能按时间顺序返回,但只要在它们上调用 allKeys 之类的东西,顺序就会消失.
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!
更新: 在 Firebase 3.0 + Swift 中,你可以使用FIRServerValue.timestamp()
.在 Objective-C 中,这是 [FIRServerValue timestamp]
.
Update: In Firebase 3.0 + Swift, you can use
FIRServerValue.timestamp()
. In Objective-C this is [FIRServerValue timestamp]
.
在 Swift 中,您现在可以在 Firebase 2.0.3+(3.0 之前)中使用 FirebaseServerValue.timestamp()
.
In Swift, you can now use FirebaseServerValue.timestamp()
with Firebase 2.0.3+ (before 3.0).
iOS 中 Firebase.ServerValue.TIMESTAMP
的等效项是 kFirebaseServerValueTimestamp
.目前,这只适用于 Objective-C 而不是 Swift.
The equivalent for Firebase.ServerValue.TIMESTAMP
in iOS is kFirebaseServerValueTimestamp
. Right now, this only works for Objective-C and not Swift.
在 Swift 中,您可以使用
In Swift, you can create your own global timestamp with
let kFirebaseServerValueTimestamp = [".sv":"timestamp"]
然后您将能够以相同的方式使用 kFirebaseServerValueTimestamp
.
and then you'll be able to use kFirebaseServerValueTimestamp
in the same way.
但你只能将它用作节点的值或优先级.您将无法将其设置为密钥名称(尽管我不相信您也可以在 Web API 中).
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).
一般来说,在字典上调用 allKeys
不会> 保证顺序. 但是,如果您在节点上使用 childByAutoID
,则可以通过按字典顺序对 allKeys
返回的 NSArray 进行排序来恢复正确的顺序.这样的事情会起作用:
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);
}];
这类似于对 NSArray 按字母顺序进行排序,但是在对自动生成的 ID,您确实不想要本地化或不区分大小写的排序,因此您使用 compare:
而不是 localizedCaseInsensitiveCompare:
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 获取服务器时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!