Frank Zappa
Frank Zappa

Reputation: 451

Firebase Date Format - Best Practice to Bridge IoS and Android

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:

enter image description here

Upvotes: 0

Views: 1744

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598765

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:

Upvotes: 2

Related Questions