Firebase Date Format - Best Practice to Bridge IoS and Android

有些话、适合烂在心里 提交于 2019-12-22 00:31:04

问题


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.

Here are my questions:

  • Although it "Works" is Java Date not supported in FB?
  • If Java Date is supported in FB, will a Swift Class with Date in it inter-operate with the Java-based Dates in Firebase? (I am new to Swift)
  • What is the recommended best practice for Date format "Universality" across IoS and Android? A long int representing UTC?


回答1:


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.

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.

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

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

Also see:

  • 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


来源:https://stackoverflow.com/questions/46556598/firebase-date-format-best-practice-to-bridge-ios-and-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!