Part II – Writing Better, Faster and Easy to Maintain Mobile Apps with Realm
Part II - Writing Better, Faster and Easy to Maintain Mobile Apps with Realm
In my previous blog post, I described Realm’s seamless synchronization between devices and applications. In this part, we are going to discuss implementation and usage of Realm.
You do not have to create a table, helper etc. in Realm. Just create POJO class, which will be a subclass of RealmObject and you are ready to start using Realm. Here is how to use Realm.
Define Model
Realm accepts all primitive types and its boxed types (except for char), String, Date and byte[]. It also supports subclasses of RealmObject and RealmList<? extends RealmObject> to model relationships.
Initialize Realm
Supports synchronous and asynchronous
Synchronous
Asnchronous
Relationships
Any two RealmObjects can be linked together.
Queries
All fetches (including queries) are lazy in Realm, and the data is never copied. Realm’s query engine uses a Fluent interface to construct multi-clause queries.
Find one
Find all
Filtering
For all data types, you have the following predicates:
- equalTo()
- notEqualTo()
Numeric data types, including Date, allow these additional predicates:
- between() (Both end points are included, i.e., it is a bounded interval)
- greaterThan()
- lessThan()
- greaterThanOrEqualTo()
- lessThanOrEqualTo()
String fields allow these additional predicates:
- contains()
- beginsWith()
- endsWith()
- like()
and one or more wildcards:
- * matches 0 or more Unicode characters
- ? matches a single Unicode character
predicates to check for emptiness:
- isEmpty()
- isNotEmpty()
Two useful predicates for matching the null value are:
- isNull()
- isNotNull()
Supports Other Libraries
GSON, Jackson-databind, Kotlin, Parceler, Retrofit, Robolectric, RxJava
Speed
This was a quick primer on how to make your mobile apps better, faster and maintainable using Realm.
Also read Introduction to Realm.
- Android
- iOS
- Mobile Apps
- Mobile Database
- Realm
Rohan Pawar
24 November 2017