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.
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.
How To use Realm
1. Create a Model.
2. Add a Row.
4. Logical Operators.