Abstracting away the database


#1

I wrote an app where Realm is used “as intended” - the Realm objects are used directly in my views, in order to use the out of the box lazy loading, etc.

Now I’m planning to port this app (iOS) to Android, where I want to do things right. One of those things is decoupling the UI from the database implementation.

The idea would be, then, to have an interface, with, say this method:

getCats(): List<Cat>

Where Cat is a plain object (not inheriting from Realm object).

The Realm implementation of getCats() would fetch the “Realm cats” from the database and map them to my plain Cat objects.

To perform updates, I’d have something like

updateCat(cat: Cat)

Where Cat contains a unique identifier, with which Realm has first to fetch it, and then update.

For anything where I need real time updates, I’d return an rx observable. The implementation would observe using Realm’s notification block, map to plain objects and notify my observable.

My question: What are the drawbacks to this approach? For example, is it expensive to fetch by id, such that my updates will be inefficient (since I first have to retrieve the Realm objects)? Will operations like changing the order of elements in a list, etc. be easily mapped to Realm’s equivalents? Etc.?