[英]How to get Firebase server time in seconds while set/update data on firestore objective-c?
We are facing an issue regarding timestamp while set/update data on firestore.在 firestore 上设置/更新数据时,我们面临有关时间戳的问题。 We want actual firebase server timestamp in seconds but by using
[[FIRTimestamp timestamp].seconds]
gives us timestamp in second but for system date not actual server date.我们想要以秒为单位的实际 firebase 服务器时间戳,但是通过使用
[[FIRTimestamp timestamp].seconds]
给我们以秒为单位的时间戳,但系统日期不是实际服务器日期。
So how can we get server timestamp on app while set/update data?那么我们如何在设置/更新数据时获取应用程序的服务器时间戳呢? We also use
[FIRServerValue timestamp]
but its gives result in dictionary format [".sv":"timestamp"]
.我们也使用
[FIRServerValue timestamp]
但它以字典格式[".sv":"timestamp"]
给出结果。 So we tried to get seconds from this dictionary by using following way:所以我们尝试通过以下方式从这本字典中获取秒数:
NSLog(@"%ld",(NSInteger)[FIRServerValue timestamp]/1000); //105827998577
But this timestamp we get is wrong.但是我们得到的这个时间戳是错误的。
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.然后您将能够以相同的方式使用
kFirebaseServerValueTimestamp
。
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.
您将无法将其设置为键名。
In general, calling allKeys on a dictionary does not guarantee order.通常,在字典上调用 allKeys 并不能保证顺序。 But if you're using childByAutoID at a node, you can get back the right order by ordering the NSArray returned by allKeys lexicographically.
但是,如果您在节点上使用 childByAutoID,则可以通过按字典顺序对 allKeys 返回的 NSArray 进行排序来恢复正确的顺序。 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:这类似于按字母顺序对 NSArray 进行排序,但是在对自动生成的 ID 进行排序时,您不希望进行本地化或不区分大小写的排序,因此您使用 compare: 而不是 localizedCaseInsensitiveCompare:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.