Firebase日期格式-桥接IoS和Android的最佳实践 [英] Firebase Date Format - Best Practice to Bridge IoS and Android

查看:87
本文介绍了Firebase日期格式-桥接IoS和Android的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个可操作的Android应用程序,用于在Firebase中存储/检索数据.我现在在IoS/Swift中复制该应用程序.在Android上,我使用包含Java Date属性的Java类(POJO).我将整个课程存储在Firebase中.以下是Firebase使用Java Date的屏幕截图.

I built an operational Android app that stores / retrieves data in Firebase. I am now replicating the app in IoS / Swift. On Android I use Java Classes (POJOs) that include Java Date attributes. I store the whole class in Firebase. Below is a screenshot of what Firebase does with a Java Date.

这是我的问题:

  • 虽然FB是不支持Java Date的,但它是可行的"吗?
  • 如果FB支持Java Date,那么其中带有Date的Swift类是否可以与Firebase中基于Java的Date互操作? (我是Swift的新手)
  • 在IoS和Android上,日期格式通用"的推荐最佳做法是什么?代表UTC的long int吗?

推荐答案

Firebase实时数据库仅存储JSON类型. JSON中没有本地Date类型.因此,您在数据库中实际看到的就是Java Date类的属性.

The Firebase Realtime Database stores JSON types only. There is no native Date type in JSON. So what you actually see in your database are the properties of the Java Date class.

老实说:我很惊讶这从一开始就行.大多数标准Java类都包含与Firebase的JSON序列化逻辑不兼容的东西.

I'll be honest: I'm quite surprised that this works to begin with. Most standard Java classes contain things that are incompatible with Firebase's JSON serialization logic.

但是以这种格式存储日期实际上是一种反模式.您存储的方式比需要的信息更多.

But storing dates in this format is really an anti-pattern. You're storing way more information than is needed.

在大多数情况下,您应该存储一个时间戳记,即距该纪元以来的毫秒数,即1507089082006.

In most cases you should store a timestamp, the number of milliseconds since the epoch, i.e. 1507089082006.

在某些情况下,将日期存储为可排序的字符串格式(即"2017-10-03")会更方便.

In some cases it may be more convenient to store the date as a sortable string format, i.e. "2017-10-03".

另请参阅:

  • How do you save a DATE field in Firebase using AngularFire
  • Store Time efficiently in Firebase Database?
  • Saving and retrieving date in Firebase
  • How to deal with [NSDate] and Firebase

这篇关于Firebase日期格式-桥接IoS和Android的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆