21-Apr-2017

Realm the Mobile Database

Rajesh K      android

As Android developers, everyone knows the database SQLite for storing information on devices. But SQLite is suitable for storing JSON or Java objects. When we are receiving JSON data from Rest API's. Sometimes Working with SQLite makes code difficult to understand, and the writing of some SQL queries can take too much time. Using ORM partially solves some problems, but today we have an alternative way, known as Realm. Realm allows us to easily store Java Objects,retreive and modify Java Objects.

Why we need to say No to ORM:

A lot of what you would call “database libraries” are simply that — third party libraries using a particular storage engine that provide a modern/functional/different (pick your favorite) approach to accessing an existing data layer.

You could use a Swift or Java library to access your SQLite storage in a more modern way by using generics and structs. But the storage engine that’s doing all of the reading and writing to disk is still SQLite.

Plus, that third-party library you use will still need to convert your native data structures to some intermediate format, execute behind-the-scenes SQL queries, and convert your data to SQL table rows.

If you are using a full-fledged ORM, you will almost always have numerous operations running in the background. An ORM will continuously convert your objects to intermediate format and run SQL queries to talk to a SQLite file. Any time you access a related object to the object you’re working with - the ORM will convert this to a JOIN SQL query, execute that query on a lookup table, and find matching records in another table where those related objects are stored.

This is a lot of work for simple data access. Not only does it take up CPU cycles and disk time, but it can become slow very quickly. That means you have to take that work off your main thread and introduce proper threading techniques, and now your code is getting rather complicated, just to get your data.

Why to choose Realm?

Realm is not an ORM, and is not built on top of SQLite. Instead They’ve built a full database for mobile app developers

It’s an easy mistake to think that Realm is built on top of an existing technology, or that it’s just a modern rewrite of another library. 

Unlike SQLite, Realm is a mobile first database engine, built from scratch using C++ core. It stores data in a universal, C++ core table-based format. Realm is an open-source, object-oriented database, built specifically for mobile devices, keeping in mind mobile device constraints (compared to desktop). 

  • Simplicity ( keep implementation simple )

  • Live realm (choice in between using live realm or close connection like in sqlite)

  • Easy to integrate (GSON, Parceler, Retrofit, Roboelectric, RxJava, Kotlin, Stetho...)

  • Build in features ( use advanced build-in features )

The Realm Mobile Database therefore isn’t related to SQLite or another SQL database; it aims instead to solve many of the issues that exist in that field. But the Realm Mobile Database isn’t a key-value store either — this kind of storage is great in certain situations, but developers really want to be able to work directly with native objects in their code.

Finally, the Realm Mobile Database isn’t an ORM. ORMs convert flat data like SQL database tables into object graphs, so that those can be used from native code. Realm’s custom storage engine doesn’t need to convert data to and from object graphs, so there’s no need for an ORM.

The Realm Mobile Database stores objects directly on disk with the least possible type or structure conversion. Since there’s no mapping or other untangling of complex entities on-the-fly, the Realm Mobile Database is able to get objects from memory to disk extremely quickly.

In fact, that’s one of the trademark features of the Realm Mobile Database: You are welcome to read and write on the main thread! You don’t have to be afraid that this will block your UI. 

Realm Core is written in super-fast C++, and it maps C++ objects to disk. Since it’s developed alongside the SDKs, its API and behavior are closely aligned to the ways that mobile apps actually use data, so you get an easy mental model of how it works.

We will further explore Realm in our next blog.