Realm is substitute of SQLite and ORM libraries in your Android application. It is a lightweight database that can replace both SQLite and ORM libraries.
Installation:-
Step 1: Add the following class path dependency to the project level build.gradle file.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "io.realm:realm-gradle-plugin:0.90.0"
}
}
Step 2: Apply the realm-android
plugin to the top of application/module level build.gradle
file.
apply plugin: 'realm-android'
From the above successfully integrated Realm in our application. Now you can use the awesome feature of Realm.
Step3: Set default configuration of Realm in application class so that you can use its instance in any activity
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();
Realm.setDefaultConfiguration(realmConfiguration);
}
}
Step 4: Create RealM data models
Create a model class and extend with RealmObject. Any Model class or JavaBean can be stored in a Realm once it extends the RealmObject
class.
public class EmployeeBean extends RealmObject {
private String employeeName;
private String employeeId;
private long employeeSalary;
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public long getEmployeeSalary() {
return employeeSalary;
}
public void setEmployeeSalary(long employeeSalary) {
this.employeeSalary = employeeSalary;
}
}
Step 5: Obtain Realm instance in activity
mRealm = Realm.getDefaultInstance();
Step 6: Store data on Realm object
Syntax:-
// Obtain a Realm instance
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
//... add or update objects here ...
realm.commitTransaction();
Add employee details on Realm like this
mRealm.beginTransaction();
EmployeeBean employee = mRealm.createObject(EmployeeBean.class);
employee.setEmployeeId("EMP001");
employee.setEmployeeName("Tarun Joshi");
employee.setEmployeeSalary(100000);
mRealm.commitTransaction();
You can also use Transaction blocks which will automatically handle begin/commit, and cancel if an error happens. That means you don't need to add beginTransaction() and commitTransaction() everytime.
// Transaction block
mRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
EmployeeBean employee = mRealm.createObject(EmployeeBean.class);
employee.setEmployeeId("EMP002");
employee.setEmployeeName("Amit Rai");
employee.setEmployeeSalary(60000);
}
});
If you want to do the above transaction in a background thread then use Asynchronous Transactions
mRealm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
EmployeeBean employee = bgRealm.createObject(EmployeeBean.class);
employee.setEmployeeId("EMP003");
employee.setEmployeeName("Devesh Bhandari");
employee.setEmployeeSalary(75000);
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
// Transaction was a success.
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
// Transaction failed and was automatically canceled.
}
});
Step 7: Retrieve result from Realm objects or how to make query using Realm
// query all employee
RealmResults<EmployeeBean> employees = mRealm.where(EmployeeBean.class).findAll();
for (EmployeeBean employee : employees) {
Log.i(TAG, "EmployeeId: "+employee.getEmployeeId());
Log.i(TAG, "EmployeeName: "+employee.getEmployeeName());
Log.i(TAG, "EmployeeSalary: "+employee.getEmployeeSalary());
}
// retrieve all employees whose sal is greater than 50000
RealmResults<EmployeeBean> employees = mRealm.where(EmployeeBean.class).greaterThan("employeeSalary", 50000).findAll();
This is sufficient information to start with Realm. Please let me know in comments for further queries.
Happy coding :)
0 Comment(s)