1. Thunderrise
  2. Blog
  3. Why to use Realm for Android Development
Why to use Realm for Android Development

Why to use Realm for Android Development

team
Thunderrise

With the rise of technologies in the last few years, a lot of tools were created to simplify developers life. These tools allow them to code with maximum performance and quality. It’s really hard now to reach the top of App Store and teaching your app to scale is a lot harder. That’s why developers often the problem of databases. It’s really annoying for all programmers and we are not an exception. Previously we had only two alternatives: SQLite and Core Data. But today we speak about Realm - the new better alternative to above mentioned.

SQLite VS Realm

Android OS usually use SQLite as a reigning database. It use SQLite Database and SQLiteOpenHelper interfaces to work with OS. It’s really time-consuming to implement even standard mechanics of database control to Android. And here come different ORMs (object-oriental mappings). It is a programming technique for converting data between incompatible type systems in object-oriented programming languages.

In 2014 the alternative to SQLite and ORMs was born. Realm is cross-platform mobile database for IOS and Android. It’s really better than SQLite because of its speed and simplicity because you can do things with only couple of code lines. Realm is a mobile-first system so it’s a new thing because we didn’t see any mobile database upgrades for a long time. Realm is not an ORM and it uses its’ own mechanism of persistence for better performance and higher speed. Experience shows that Realm outstrips not only SQLite but other ORMs for Android like GreenDAO and ORMLite.

But Realm is not an ORM at all. It has its’ own engine that works separately from SQLite or other databases. It’s not used only in Java for Android but in IOS development too. So this article can be also useful for development on Swift and Objective-C.

Why to use Realm

As we’ve decided above, the main reason is speed. Realm is 10 times faster than SQLite. Collaboration with database is directly proportional to the response of application. If you want your app to react swiftly on users’ request, Realm is the greatest solution for you.

The other essential aspect is that Realm is really fast to integrate and easy to work with. You don’t have to spend much time and efforts on implementation and maintenance. It is available because of good supporting documentation and low entry level. Therefore, less time and efforts is spent on app creation and the whole project becomes simpler.

Realm is a totally reliable product. A lot of top companies use it for enterprise applications, healthcare platforms, education platforms and other software. And the very good addition to these advantages is that Realm is absolutely free.

How to install

In order to start using Realm for Android development, just add “classpath "io.realm:realm-gradle-plugin:3.3.0" compile to your build.gradle (Project), and “apply plugin: 'realm-android'” to the top of application level build.gradle (Module) file.

buildscript {

repositories {

jcenter()

}

dependencies {

classpath "io.realm:realm-gradle-plugin:3.3.0"

}

}

That is how to initialize Realm to your application project:

public class MyApplication extends Application {

@Override

public void onCreate() {

super.onCreate();

Realm.init(this);

RealmConfiguration config = new RealmConfiguration.Builder().build();

Realm.setDefaultConfiguration(config);

}

}

And then it has to be initialized to Activity

public class MyActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Realm realm = Realm.getDefaultInstance();
try {

// ... Do something ...

}finally {

realm.close();

}

}​

If you want to augment your database, just call

Rlm realm = Realm.getInstance(this);

realm.beginTransaction();

//... add or update objects here ...

realm.commitTransaction();

And to cancel it:

realm.cancelTransaction();

To set up an object in Realm database, this object's’ class has to be extended from RealmObject:

public class “classname” extends RealmObject {

@PrimaryKey

private long id;

private String name;

//Getters & Setters

}

When you create new Objects to databale you must remember, that Realm not support auto-incrementing IDs, and because of it you will need to query for the max value each time you begin a transaction :

Realm realm = Realm.getInstance(this);

realm.beginTransaction();

Number maxValue = realm.where(classname.class).max("id"); //Search maximum ID

long lastId = (maxValue != null) ? (maxValue.longValue()+1) : 0; //If number is not null, autoincrement, else - set 0

“classname” yourFirstObject = realm.createObject(classname.class, lastId++);// Create Object, and set last ID

yourFirstObject.setName(“Chris”);

“classname” yourSecondObject = realm.createObject(classname.class, lastId++);

yourSecondObject.setName(“Nancy”);

realm.commitTransaction();

However, for not using that, you can provide unique identifier for every Object:

@PrimaryKey

private String id = UUID.randomUUID().toString();

Realm should automatically update to the last version with every database change. It’s a really good feature that allows to save the latest version of UI without unnecessary effort. If automatic update is not enough, you can receive a callback after every change:

RealmResults<...> results = realm

.where(classname.class)

.equalTo("id", “id needed”);

results.addChangeListener(() -> {

//handle callback

});

The only drawback of this way is that we don’t actually know where the changes are done.

Summing up

As a conclusion it’s needed to say that Thunderrise developers really liked to test Realm for their coding. This cross-platform database can boast with its’ really high speed. Realm is also really convenient and developer-friendly so it’s a good tool both for newbies and advanced developers. If there are some questions, just contact us and we will answer all of them.