Repository
https://github.com/realm/realm-java
What Will I Learn?
- How to use RealmConfiguration
- Realm Aggregation
- sum()
- min()
- max()
- average()
- minDate()
- maxDate()
Requirements
- An Integrated Development Environment(IDE) for building Android Application(e.g Android Studio, IntelliJ)
- Android Device/Virtual Device.
- Little Experience in working with Realm Java.
- Java Programming Experience.
- Of course, willingness to learn
Resources
- Retrofit Website. https://realm.io/
- Retrofit Github. - https://github.com/realm
- Retrofit License - Apache License
- Lombok Website - https://projectlombok.org
Difficulty
- Intermediate
Tutorial Duration - 30 - 35Mins
Tutorial Content
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.
Outline
- Dependencied Used
- Add a TextView in activity_main.xml layout file.
- Create a model class - Person
- Realm Aggregation Illustration
Depenedencies used
- implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
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.
- implementation 'org.projectlombok:lombok:1.16.20'
annotationProcessor 'org.projectlombok:lombok:1.16.20'
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 :
Add TextView in activity_main.xml
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"
.
Creating the Person model Class
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;
}
Realm Aggregation Illustration
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() - takes a model field as the first argument (i.e age ) and returns the object having the smallest age.
- max() - Does the opposite of
min()
which returns the object with the highest age. - sum() - takes a model field as its first argument (i.e age ) and returns the sum of all the age's returned in the RealResult object.
- average() - takes a model field as the first argument and returns the average of all the specified field in the RealmResult object.
- maxDate() - takes a Date model field as its first argument and then returns the object with the highest Date field.
- minDate() - does the opposite of
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
- We initialize Realm in our Acitivy class file -
Realm.init(this);
- We create a custom Realm configuration by the name -
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. - We then call the createRealmObjects(); which as its name implies creates Realm Objects.
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
- We start an
executeTransaction()
on our realm variable and then we override theexecute()
method where we create six newPerson
objects with the details- Object 1 (Paul) => name = Paul , age = 27 , dateOfBirth = May 05 2006
- Object 2 (Bola) => name = Bola , age = 31 , dateOfBirth = June 06, 2001
- Object 3 (Sam) => name = Samuel , age = 15 , dateOfBirth = Feb 02, 2010
- Object 4 (Blee) => name = Blessing , age = 12 , dateOfBirth = Jan 01, 2008
- Object 5 (Isi) => name = Israel , age = 19 , dateOfBirth = Dec 12, 2012
- Object 6 (Samanta) => name = Samanta , age = 52 , dateOfBirth = Nov 11, 2011
- Next, we declear a StringBuilder object - toDisplay.
- We then get all the objects that are of
Person
class and store them in a RealResult object - results - Next, we get the sum, min , max and average of the objects as below -
- sum() : We call the
sum()
method on theresult
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() : We call the
min()
method on theresult
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() : We call the
max()
method on theresult
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() : We call the
average()
method on theresult
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.
- sum() : We call the
- We also get the maxDate and minDate as below
- maxDate() : We call the
maxDate()
method on theresult
RealmResult object and specify thedateOfBirth
field as its first argument and we do the same for the minDate but calling theminDate()
method on theresult
RealmResult object.
- maxDate() : We call the
- We then get the youngest , oldest , firstCelebrator and -lastCelebrator_
Person
object as explained below -- youngest : We use the
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 thefindFirst()
method which will return aPerson
object that is the youngest. - oldest : We use the
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 thefindFirst()
method which will return aPerson
object that is the youngest. - firstCelebrator : We use the
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 thefindFirst()
method which will return aPerson
object. - lastCelebrator : We use the
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 thefindFirst()
method which will return aPerson
object.
- youngest : We use the
- We then display the details of all this
Person
object appending them to the -toDisplay
StringBuilder object as below :- name : (i.e youngest.getName)
- age : (i.e oldest.getAge)
- dateOfBirth : (i.e firstCelebrator.getDateOfBirth)
- Lastly, we set the toDisplay StringBuilder object as the text of our TextView injected with ButterKnife -
resultTv.setText(toDisplay);
Application Execution Image
Curriculum
- Understanding Queries in Realm Java using Android Studio (PART 1)
- Understanding Queries in Realm Java using Android Studio (PART 2)
- Understanding Queries in Realm Java using Android Studio (PART 3)
- Understanding Queries in Realm Java using Android Studio (PART 4)
- Understanding Queries in Realm Java using Android Studio (PART 5)
- Understanding Queries in Realm Java using Android Studio (PART 6)
- Understanding Migrations in Realm Java using Android Studio
Proof of Work
https://github.com/generalkolo/Realm-Examples/tree/master/Realm%20Aggregation
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!Hey @edetebenezer
Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!
I see...
I really want to learn all of them.... Thank you darling... You are a good person.. . You're the best
Am glad you like it @asrinaphonna
Thank you for your contribution.
I liked overall the concepts discussed herein although a bit basic, but had few suggestions/questions:
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post, click here.
Chat with us on Discord.
[utopian-moderator]Need help? Write a ticket on https://support.utopian.io/.
Thanks @mcfarhat for taking out time to moderate my contribution.
Just thought I would reduce the length of the codes pasted so I showed in the comments that another object was created using the format of the one showed (object - Paul)
I wanted to show the use of the maxDate and max differently that was why that was done.
I would rectify this accordingly
I would also look into this.
Thank you once again.
Congratulations @edetebenezer! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word
STOP