Android Working with Realm Database — Replacing SQLite & Core Data
What is Realm?
Realm is a mobile database and a replacement for SQLite. Although is an OO database it has some differences with other databases. Realm is not using SQLite as it’s engine. Instead it has own C++ core and aims to provide a mobile-first alternative to SQLite. Realm store data 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 ad hoc queries.
Below are the advantages of Realm over SQLite:
> faster than SQLite (up to 10x speed up over raw SQLite for normal operations)
> easy to use
> object conversion handled for you
> convenient for creating and storing data on the fly
> very responsive team
Also there are some disadvantages which might be taken into consideration:
> no importing
> still under active development
> not a lot of content online
> can’t access objects across threads
Realm at the moment is missing the following features:
> null support
> auto incrementing id’s
> Map support
> easy migrations
> notifications on specific data changed
> compound primary keys
Prerequisites:
- Realm is supported in Android only, not available for Java at the moment.
- Android Studio version 0.8.6
- JDK version 7
- Android API Level minimum 9 (Android 2.3 Gingerbread)
Installation
Add compile ‘io.realm:realm-android:0.83.0+’ in your build.gradle and sync a project. Or, you can download a realm-VERSION.jar and add it into app/libs.
Models
If you want a class to be stored in Realm, it should extend from RealmObject. Realm supports the following field types: boolean, byte, short, ìnt, long, float, double, String, Date. Moreover, subclasses of RealmObject and RealmList<? extends RealmObject> support model relationships.
The boxed types Boolean, Byte, Short, Integer, Long, Float, and Double can also be used in model classes. With their help, you can set the value of a field to null.
Annotations
@Required — tells Realm to enforce checks that disallow null values.
@Ignore — supposes that a field should not be persisted to disk.
@Index — will add a search index to the field. Due to this queries will be faster, although inserts will become slower and the data file will get larger.
@PrimaryKey — supposes that the field is indexed.
Start Using Realm
Initialize Realm in Application:
public class MyApplication extends Application {@Overridepublic void onCreate() {super.onCreate();RealmConfiguration config = new RealmConfiguration.Builder(context).build();Realm.setDefaultConfiguration(config);}}
Initialize Realm in Activity:
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Realm realm = Realm.getDefaultInstance();}}
Writing in the Database
If you want to write something in the database you just need to call:
Realm realm = Realm.getInstance(this);realm.beginTransaction();//... add or update objects here ...realm.commitTransaction();
You always have an opportunity to cancel your transactions, just call:
realm.cancelTransaction();
Creating Objects-( University)
If you want to save an object in the Realm database, the class of that object should extend RealmObject
public class University extends RealmObject {@PrimaryKeyprivate String id;@Requiredprivate String name;private RealmList students;// getters and setters}
University will be able to add a university, delete a university by id, get a university by id from the database, etc
Adding Data in University
Realm realm = Realm.getInstance(SimpleRealmApp.getInstance());realm.beginTransaction();University u = realm.createObject(University.class);u.setId(UUID.randomUUID().toString());u.setName(university.getName());realm.commitTransaction();
Deleting Data from University
Realm realm = Realm.getInstance(SimpleRealmApp.getInstance());realm.beginTransaction();University university = realm.where(University.class).equalTo(RealmTable.ID, Id).findFirst();university.removeFromRealm();realm.commitTransaction();
Get All University data
Realm realm = Realm.getInstance(SimpleRealmApp.getInstance());RealmQuery query = realm.where(University.class);RealmResults results = query.findAll();