ios - 对 kFirebaseServerValueTimestamp 执行数学运算

标签 ios objective-c firebase

在这篇文章的评论之一中:firebaseArray descending order? ,一位 Firebase 员工建议将时间戳乘以 -1,以便按日期降序排序。

这是我正在尝试实现的一个想法,但是,我很难做到这一点,因为 kFirebaseServerValueTimestamp 是一个 NSDictionary,键为 ". sv”和“timeStamp”的值。

当我尝试隔离该值时,我所能得到的只是一个显示“timeStamp”的 NSString

有没有办法对 kFirebaseServerValueTimestamp 执行数学运算?

最佳答案

kFirebaseServerValueTimestamp 是一个真正用作 firebase 在写入数据时填充的占位符的属性。

来自文档:

Server Values - Placeholder values you may write into a Firebase database as a value or priority that will automatically be populated before writing to the Firebase database.

kFirebaseServerValueTimestamp - The number of milliseconds since the Unix epoch

例如,如果您想将时间戳基于 Firebase 服务器时间,那么

Firebase *timeStampsRef = [self.myRootRef childByAutoId];

[timeStampsRef setValue:kFirebaseServerValueTimestamp];

Firebase *timeStampsRef2 = [self.myRootRef childByAutoId];

[timeStampsRef2 setValue:kFirebaseServerValueTimestamp];

会产生以下结果

-K861VyNBisoAL14dHsk: 1452890788194
-K861VyNBisoAL14dHsl: 1452890788195

如果您想对 Firebase 数据进行排序,您应该包含一个时间戳属性作为您要排序的节点的子节点。

pizza_time
  node_id_0
   timeStamp: 20160109153000
   location: "Pizza House"
  node_id_1
   timeStamp: 20160110153000
   location: "Pizza Place"
  node_id_2
   timeStamp: 20160114153000
   location: "Pizza Busters"

然后您可以按日期排序、从一个日期到另一个日期查询节点或各种其他数据技巧。

关于ios - 对 kFirebaseServerValueTimestamp 执行数学运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34816024/

相关文章:

ios - 检测首次在 iOS 中安装 NOT NSUserDefaults

ios - 如何在 UIBarButton 项目下面添加一个 UIImage

web-applications - 使用 "no backend"编写 Web 应用程序

ios - 在 swift3 中通过 Alamofire 发出请求时出现 404 错误

iphone - UIImagePickerController 中的自定义裁剪尺寸

ios - 我的 searchBar 过滤器无法正常工作

iphone - 将 iPhone 应用程序转换为通用应用程序

ios - 不使用桥接 header 访问私有(private) UIKit 函数

javascript - 如何验证 firebase 用户当前密码

firebase - 在 Firestore 中获取文档名称