問題描述
我有一個(gè)使用 Firebase 的 iOS 應(yīng)用,目前有一些字典,其中的鍵是 NSDate 對(duì)象.這樣做的一個(gè)明顯問題是 NSDate 從設(shè)備的系統(tǒng)時(shí)間中提取,這不是通用的.
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 獲取服務(wù)器時(shí)間戳(類似于 Web API 的 Firebase.ServerValue.TIMESTAMP)以便我可以按時(shí)間順序?qū)ψ值滏I進(jìn)行排序的最佳方法是什么?
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 的時(shí)間順序,但我無法找出在代碼中對(duì)它們進(jìn)行排序的正確方法.雖然它們可能按時(shí)間順序返回,但只要在它們上調(diào)用 allKeys 之類的東西,順序就會(huì)消失.
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.
任何有關(guān)此問題的幫助將不勝感激!
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 中,您現(xiàn)在可以在 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
的等效項(xiàng)是 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.
但你只能將它用作節(jié)點(diǎn)的值或優(yōu)先級(jí).您將無法將其設(shè)置為密鑰名稱(盡管我不相信您也可以在 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).
一般來說,在字典上調(diào)用 allKeys
不會(huì)> 保證順序. 但是,如果您在節(jié)點(diǎn)上使用 childByAutoID
,則可以通過按字典順序?qū)?allKeys
返回的 NSArray 進(jìn)行排序來恢復(fù)正確的順序.這樣的事情會(huì)起作用:
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);
}];
這類似于對(duì) NSArray 按字母順序進(jìn)行排序,但是在對(duì)自動(dòng)生成的 ID,您確實(shí)不想要本地化或不區(qū)分大小寫的排序,因此您使用 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:
這篇關(guān)于如何從 Firebase 的 iOS API 獲取服務(wù)器時(shí)間戳?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!