underlx / underlx

Unofficial Lisbon Metro Android app. It is kind of a client for https://github.com/underlx/disturbancesmlx

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Replace Realm with Room

gbl08ma opened this issue · comments

Realm is quite nice and very easy to work with, however, it has a binary (native) core that noticeably increases the APK size, while being merely a convenience for the programmer (it has no effect on the features or end user experience). Furthermore, this binary core is not open source yet.

Using SQLite was initially an option, however, the amount of boilerplate and lack of compile-time verification of code (as well as auto-complete, etc.) makes using SQLite with the vanilla APIs a bit painful.

Room apparently makes using SQLite much easier, while still being able to harness the full power of SQL (something lost with Realm). It almost certainly doesn't add to the APK size as much, and might actually use less resources at run-time.
The model classes underlx already has, for use with Realm, can probably be made into Room entities with little change.

Replacing Realm with Room/SQLite appears to be feasible and very appealing.

Data migration

Ideally we would like to avoid losing the trip history that beta testers have been collecting over the past months. This means a migration path from Realm to Room/SQLite must be established.
The only way to do this is to ship one or two releases that keep the Realm dependency solely for the purpose of migrating the data from the Realm database into SQLite, and then the Realm database is deleted. Realm would not be used for anything else in these releases. In a later release, Realm would be removed completely along with the migration code (the Realm database deletion code can still be kept).

This means users who update directly from the current version to that later version, that no longer contains Realm or the migration code, would lose their data. For an app with less than 30 users, with many of them probably not having that many trips in their history, this seems like a good compromise.

One of the difficulties that come to mind with this approach is that as long as Realm is present, the model classes can't be removed or replaced with Room entities (unless there's a way to use the same classes for both Realm and Room, but probably not). It's probably best to introduce a different package for Room entities, then the current model package will be deleted.

I started playing around with Room, but it takes more effort to migrate than I expected. The fact that Room doesn't allow object references like Realm means some things need to be rethought and more code rewritten. The data model must be thought with relational databases in mind, instead of OOP object composition. Not very surprising, since SQLite is still underneath the whole thing, and everything in Room very clearly maps to SQLite structures.

There's some stuff on the room-experiment branch.

Since the priority right now is to get the most important features going so we can go public on the Play Store, and since the database can be more or less flawlessly migrated later, and as what we have right now works, this shall be put on hold for now.