简体   繁体   English

如何在 firestore objective-c 上设置/更新数据时以秒为单位获取 Firebase 服务器时间?

[英]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.

相关问题 如何从 Firestore 获取特定数据并在 Objective-C 中更新它 - How to fetch specific data from Firestore and update it in Objective-C 如何从Flutter中的云Firestore中获取、设置、更新和删除数据? - How to get, set, update and delete data from the cloud firestore in Flutter? 一段时间后更新 Firebase Firestore 数据 - Update Firebase Firestore Data After A Certain Amount of Time 更新服务器时间戳 - firestore - Update Server time stamp - firestore 如何实时更新 firestore 数据 - how to Real Time update of firestore data 使用 flutter 中的时间戳或 firebase 服务器时间从 firestore 保存和查询数据 - save and query data from firestore with timeStamp or firebase server time in flutter 在 Firebase Firestore 中获取特定嵌套数据的实时更新 - Get real-time updates on specific nested data in Firebase Firestore 如何更新数据存储在 firebase firestore 中的小部件的可见性? - How to update the visibility of widgets whose data is stored in firebase firestore? 从 Firebase firestore 查询数据时如何在 stream 内部等待 - How to await inside a stream while querying data from Firebase firestore iOS/Objective-C:如何在 Firebase Crashlytics 中导入 DWARF 文件? “${PODS_ROOT}/FirebaseCrashlytics/upload-symbols”不再工作 - iOS/Objective-C: How to import DWARF file in Firebase Crashlytics? "${PODS_ROOT}/FirebaseCrashlytics/upload-symbols" not working anymore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM