Using Realm Mobile Database in Android.

Can the Realm be a replacement of SQLite? I think "Yes". Here is why.

Issues using SQLite in Android
1. Not Fast Enough.
When building a listView in Android using a local mobile database, an adapter of the listView cannot handle a big list. For example, we built an app to view 37,000 contacts in a listView. Scrolling of the listView wasn't smooth. The app often stops when you scroll down the list. There was no real solution in terms of enhancing scrollability from either adapter or SQLlite side.

2. Cursor is not flexible.
In order to use SQLite, you should use a Cursor as a result object. We often face modifying the cursor to display UI. Actually, you can't modify the cursor. For example, you have firstname, lastname and phone number displayed on a contact item in a listView. Let's say users can edit the phone number in a contact detail page. When the users come back to the listView after editing, you have two options to build the listView again to reflect the change:
- query the whole 35k contacts from SQLite and build the listView again,
- or instead of querying it, just update the cursor of one row as user edited.
Using SQLite in conjunction with cursor, you cannot do the second option. This is not a flexible platform.

3. Not a Best Solution.
There is an advantage of having SQLite in terms of performing many familiar queries like join, by group, and etc. However, you need to create schema, dbhelper, and possibly provider and loader to maximize the use of SQLite in Android. This is time taking process when you simply want to add a new table into SQLite.


Realm Advantages
1. Super Fast!
Using Realm, we didn't have any lagging or slowness of scroll on top of 37,000 contacts in the listView. It's because the Realm is not an ORM on top of SQLite. Instead it uses its own persistence engine, built for simplicity (& speed). Thanks to its zero-copy design, Realm is much faster than an ORM, and is often faster than raw SQLite as well.

2. Objective-based Coding.
You don't feel like it's a database. You can simply create a model(object) and insert data using the object. You can retrieve the data by calling the same object in conjunction with simple logical operators in stead of SQL query.

3. Easy to Use!
It took us a few hours to replace the existing SQLite with Realm. The most of time taken was removing SQLite part. Adding Realm was so easy and fast. You can get started with Realm in minutes, port your app in hours, & save weeks on your project.

4. Cross-platform.
Realm supports iOS & OS X (Objective-C & Swift) &Android. You can share Realm files across platforms, use the same high-level models for Java, Swift & Objective-C, and write similar business logic on all platforms.


How To use Realm
First, add compile 'io.realm:realm-android:0.84.1' to the dependencies of your project. You are ready to use Realm. Here are some sample codes.

1. Create a Model.
public class User extends RealmObject {

    @PrimaryKey
    private String          name;
    private int             age;

    // Standard getters & setters generated by your IDE…
    public String getName() { return name; }
    public void   setName(String name) { this.name = name; }
    public int    getAge() { return age; }
    public void   setAge(int age) { this.age = age; }
}

2. Add a Row.
realm.beginTransaction();
User user = realm.createObject(User.class); // Create a new object
user.setName("John");
user.setEmail("john@corporation.com");
realm.commitTransaction();

3. Query.
// Build the query looking at all users:
RealmQuery<User> query = realm.where(User.class);

// Add query conditions:
query.equalTo("name", "John");
query.or().equalTo("name", "Peter");

// Execute the query:
RealmResults<User> result1 = query.findAll();

// Or alternatively do the same all at once (the "Fluent interface"):
RealmResults<User> result2 = realm.where(User.class)
                                  .equalTo("name", "John")
                                  .or()
                                  .equalTo("name", "Peter")
                                  .findAll();

4. Logical Operators.
RealmResults<User> r = realm.where(User.class)
                            .greaterThan("age", 10)  //implicit AND
                            .beginGroup()
                                .equalTo("name", "Peter")
                                .or()
                                .contains("name", "Jo")
                            .endGroup()
                            .findAll();


Reference
https://realm.io






Labels: , ,