https://github.com/realm/realm-java
In today's tutorial, we are going to be learning about aggregation in Realm.
In Realm, there are some default methods that in Realmm that makes performing mathematical operations on a RealmResult object easy based on a specific.
For instance, you have 8 Person objects that have the fields - name (String) , age (int) and sex (String) , we can find the oldest object using the max("age") method or the cummulation of the entire object's age by - sum("age").
And in our today's tutorial, we are going to be doing just that, we are going to be creating an android application have several objects and then we will be learning how to use this methods that are embedded in Realm.
The ButterKnife dependency should be placed in your application level gradle file - "build.gradle" which will make the injection of views (e.g ImageView, TextView) as easy as possible which we will be seeing in this tutorial.
The lombok dependency also is placed in the application level gradle file which makes the generator of getter and setter methods for our model classes by just adding the annotations @Getter for getters and @Setter for the setter methods.
Realm dependency
Steps
Head to your project level gradle file and add the classpath dependency:
classpath "io.realm:realm-gradle-plugin:5.1.0"
Next, head to your application level Gradle file "build.gradle" and add the realm-android plugin to the top of the file.
apply plugin:'realm-android'
Finally, refresh your Gradle dependencies.
After you have added the necessary dependencies, your application level Gradle file should look like this :
And your project level Gradle file should look like this :
In order for us to show the result of our migrations in our tutorial, we are going to be adding one TextView in our activity_main.xml file :
<?xml version="1.0" encoding="utf-8"?>
//RootLayout - RelativeLayout
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="17sp"
android:textColor="#000"
android:text="Hello World!" />
Code Explanation
The above code includes a TextView in our layout file with the id - result and has a padding of 10 on every side - android:padding="10dp".
Next, we are going to create a new java class file which can be done by right-clicking on java folder => New => Java class and then input the name the class Person.
Step 1
Step 2
Next, let the Person class extend the RealmObject class and add the following fields - name (String) , age (int) and then we add the annotations @Getter and @Setter using lombok in order to inject our getter and setter methods, this way boilerplate are eliminated.
@Getter
@Setter
public class Person extends RealmObject {
private String name;
private int age;
private Date dateOfBirth;
}
To illustrate Aggregation in Realm, we are going to be creating a Realm database that will be containing several Person realmm objects and then we are going to be querying the database to retrieve details such as the oldest person in the realm database by specifying the age field mostly.
Note: Aggration in Realm are carried out on a RealmResult object.
The Aggregation methods are explained thus:
min() which returns the object with the highest age.maxDate().MainActivity.java
Firstly, we have to create a Realm variable - private Realm realm; and then add the following codes in our onCreate() method
Realm.init(this);
RealmConfiguration customConfig = new RealmConfiguration.Builder()
.name("aggregation.realm")
.build()
realm = Realm.getInstance(customConfig);
createRealmObjects();
Code Explanation
Realm.init(this);customConfig then we specify the name of our realm file as - aggregation.realm and not the default - realm.realm name and then lastly we call the build() method.createRealmObjects()
private void createRealmObjects() {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Person Paul = realm.createObject(Person.class);
Paul.setName("Paul");
Paul.setAge(27);
Paul.setDateOfBirth(new Date("May 05, 2006"));
//New Person Object Bola
//New Person Object - Sam
//New Person Object - Blessing
//New Person Object - Isi
//New Person Object - Samanta
}
});
StringBuilder toDisplay = new StringBuilder();
RealmResults<Person> results = realm.where(Person.class).findAll();
int sum = results.sum("age").intValue();
int min = results.min("age").intValue();
int max = results.max("age").intValue();
double average = results.average("age");
Date maxDate = results.maxDate("dateOfBirth");
Date minDate = results.minDate("dateOfBirth");
Person youngest = results.where().equalTo("age",min).findFirst();
Person oldest = results.where().equalTo("age",max).findFirst();
Person firstCelebrator = results.where().equalTo("dateOfBirth",minDate).findFirst();
Person lastCelebrator = results.where().equalTo("dateOfBirth",maxDate).findFirst();
toDisplay.append("Statistics of Objects in our Database --- \n\n");
toDisplay.append("The youngest Person is "+youngest.getName()+" with age : "+youngest.getAge()+" and DOB of "+youngest.getDateOfBirth()+"\n\n");
toDisplay.append("The oldest Person is "+oldest.getName()+" with age : "+oldest.getAge()+" and DOB of "+oldest.getDateOfBirth()+"\n\n");
toDisplay.append("The person with the earliest Date of Birth "+firstCelebrator.getName()+" with age : "+firstCelebrator.getAge()+" and DOB of "+firstCelebrator.getDateOfBirth()+"\n\n");
toDisplay.append("The person with the lastest Date of Birth "+lastCelebrator.getName()+" with age : "+lastCelebrator.getAge()+" and DOB of "+lastCelebrator.getDateOfBirth()+"\n\n");
toDisplay.append("The sum of all age's is "+sum+"\n\n");
toDisplay.append("The average of all age's is "+average+"\n\n");
resultTv.setText(toDisplay);
Code Explanation
executeTransaction() on our realm variable and then we override the execute() method where we create six new Person objects with the details
Person class and store them in a RealResult object - resultssum() method on the result RealmResult object and specify the age field as the first argument indicate that we want it to return an integer value with - .intValue() which returns the sum of all the age's in the result object.min() method on the result RealmResult object and specify the age field as the first argument indicate that we want it to return an integer value with - .intValue() which returns the smallest age value in the result object.max() method on the result RealmResult object and specify the age field as the first argument indicate that we want it to return an integer value with - .intValue() which returns the highest age value in the result object.average() method on the result RealmResult object and specify the age field as the first argument indicate that we want it to return an integer value with - .intValue() which returns the average of all the age's in the result object.maxDate() method on the result RealmResult object and specify the dateOfBirth field as its first argument and we do the same for the minDate but calling the minDate() method on the result RealmResult object.Person object as explained below -
result object and get the youngest person in the result set by specifying the age field and the min int variable as the second argument with the findFirst() method which will return a Person object that is the youngest.result object and get the oldest person in the result set by specifying the age field and the max int variable as the second argument with the findFirst() method which will return a Person object that is the youngest.result object and get the person object that has the earliest DOB in the result set by specifying the dateOfBirth field and the minDate Date variable as the second argument with the findFirst() method which will return a Person object.result object and get the person object that has the last DOB in the result set by specifying the dateOfBirth field and the maxDate Date variable as the second argument with the findFirst() method which will return a Person object.Person object appending them to the - toDisplay StringBuilder object as below :
resultTv.setText(toDisplay);Application Execution Image
https://github.com/generalkolo/Realm-Examples/tree/master/Realm%20Aggregation