I came across this problem when I was building the first multi-platform app at My Day To-Do . I am talking about My Simple Notes which was first out for Android and then I was building it for iOS. It’s a simple app that lets you make notes, with a note text, title and a timestamp. This is a multi-platform app i.e. the iOS, Android and the web versions all use the same database. The issue was the timestamp was being saved as a 64-bit integer and there was no date data consistency when using across these platforms. In this post, I will talk a little bit about the problem as well as present the code for the solution to it.

How does it look?

Have a look at the screenshots for the web, Android and iOS versions. Fyi, the Android/web app are a hybrid app built using Ionic framework (with Angular).

The web version

The Android version

iOS version

p.s. I am well aware of the date format inconsistency between the iOS version when compared to Android and web. Also, I can see the layout or the fonts don’t quite match either but again fixing that is a lower priority right now.

I hope the above screen demonstrates how the date and time values are shown when the note was displayed. Hence during the phase of the iOS app development lifecycle, when I got to the point of saving the data, I noticed something.

Problem

I didn’t pick it up at first but after saving about 3 to 5 notes from my iOS device, I casually opened the app on my browser. In it, I saw the notes saved from my iOS device YAY! However after saving a few more notes, I noticed that while the note title and content matched between iOS and web/Android, the date did not. The date actually made no sense which got me thinking? Exactly how was I saving the date on the Android/web side of things? Here’s how

  this.note = {
    title: "",
    addedAt: new Date().getTime(),
    text: ""
  };

This is what that data looks like on our Firebase.Firestore database

On iOS side of things, I was saving the date using Date().timeInervalSince1970 method. On investigating more, I found out more about the Javascript Date.getTime(), here’s an excerpt from MDN web docs about  Date().getTime()

The getTime() method returns the number of milliseconds* since the Unix Epoch.

* JavaScript uses milliseconds as the unit of measurement, whereas Unix Time is in seconds.

Solution

Since the Android and web version had already been released the solution had to be the iOS app saving data compitable with those versions. Hence, I created a Date extension in Swift with the following variable and method.

extension Date {
    //time interval in milliseconds since 1970
    var jsGetTimeEquivalent:Int64 {
        return Int64((self.timeIntervalSince1970 * 1000.0).rounded())
    }
    func jsToSwiftTime() -> Double {
        return Double(jsGetTimeEquivalent/1000)
    }
}

To create a new note, we use

init(title: String, body: String) {
    self.title = title
    self.text = body
    self.addedAt = Double(Date().jsGetTimeEquivalent)        
}

To display the note, we use

note?.addedAt = Date().jsToSwiftTime()

Like the blog? Subscribe for updates

Summary

Anyway, this concludes this short post on maintaining data consistency across multi-platform mobile apps.The solution wasn’t hard at all, it just required that I take a step back and carefully isolate the problem and find a solution. All in all, developing cross-platform apps is fun 🙂 It does present some unexpected and unknown challenges.

As usual, if you find any of my posts useful, support us by  buying or even trying one of our products and leave us a review on the app store.

‎My Day To-Do - Smart Task List
‎My Day To-Do - Smart Task List

‎Snap! I was there
‎Snap! I was there
Developer: Bhuman Soni
Price: $3.99
Numbers Game: Calculation Master
Numbers Game: Calculation Master
‎Simple 'N' Easy Task List
‎Simple 'N' Easy Task List
‎Captain's Personal Log
‎Captain's Personal Log
Developer: Bhuman Soni
Price: $4.99
My Simple Notes
My Simple Notes
Developer: Bhuman Soni
Price: Free
Featured on Feedspot

My Day To-Do blog has been featured as one of the top 30 Swift programming blogs on the web 🙂 So checkout Feedspot!


Leave a Reply

Your email address will not be published. Required fields are marked *