BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Realm Brings SQLite Alternative to Android

Realm Brings SQLite Alternative to Android

Lire ce contenu en français

Realm is an open-source, object oriented, mobile database. Last month, Realm for Android was released, less than 3 months after the iOS version was first available.

As we wrote earlier, Realm does not use SQLite as it's engine. Instead has it's own C++ core and aims to provide a mobile-first alternative to SQLite. And this alternative is now available on Android. 

The Realm API is currently a bit different for Android compared to iOS, reflecting the inherent differences in Objective C and Java. The Java API is much more strong-typed and fluent.

For instance, on iOS, querying happens via static methods such as "objectWhere" which return an RLMArray. The predicates are completely string-based. This is how you can query in Objective C -

RLMArray *tanDogs = [Dog objectsWhere:@"color = 'tan' AND name BEGINSWITH 'B'"];

On the other hand, this is how you would perform the same query in Java for Android -

RealmResults<Dog> tanDogs = realm.where(Dog.class)
                                  .equalTo("color", "tan")
                                  .beginsWith("name", "B")
                                  .findAll();


However, this might get replaced with a text-based query language that would work across iOS, Android and other platforms. Tim Anglade, head of product at Realm, indicated in an email exchange, that the direction the API takes will largely depend on community feedback.

Although Realm is an object oriented database, it has some key differences with other OO databases. Tim explains that while it does store data in a native object format, the objects are not stored as-is with their language specific layout and just marshalled to the disk. Instead, objects are stored in a universal, table-based format by a C++ core. This is what allows Realm to allow data access from multiple languages as well as a range of adhoc queries.

An advantage of this hybrid object/table approach over a relational database is that it also allows for efficient graph querying - even deeply nested object graphs on relatively older smart phones without impacting response times. Benchmarks published by Realm claim, on average, upto 10x speed up over raw SQLite for normal operations.

If you have currently have an app that uses CoreData (on iOS) or SQLite (on Android) and want to migrate to Realm, there is definitely some work involved. It is not a drop-in change - Tim explains that CoreData and SQLite prefer a very normalised-form that doesn't necessarily work the best with Realm. It's better to rethink your schema and model it as objects.

Once you've modified the schema to work well with Realm however, it is much easier to migrate any existing data from CoreData/SQLite. Just setup a migration (from version 0 to 1 of your Realm database), and in this migration, load your SQLite data into Realm objects and then just save them.

Or, if your data also resides on a remote server, you could just build the Realm database from scratch.

Before adopting Realm though, note that it must be considered as bleeding edge software, with an API that can have breaking changes in future versions.

You can learn more about Realm for Android or check out the code on Github

Rate this Article

Adoption
Style

BT