6. Installation – Android Studio
Maven
1. Make sure your project uses jcenter as a dependency
repository.
2. Add compile 'io.realm:realm-android:0.87.2' to the
dependencies of your project
3. Then Sync Project with Gradle Files
Jar
1. Dowload the release package and unzip
2. Copy the realm-VERSION.jar folder into app/libs
3. Then Sync Project with Gradle Files
6
7. Installation - Eclipse
1. Dowload the release package and unzip
2. Copy the jar file and folders (containing the librealm-jni.so
files) from distribution/eclipse/ to your app’s libs folder.
3. Right click on the realm jar file from libs folder, and go to
“Build Path” -> “Add to Build path”.
4. Right click your project and select “Properties”, go to “Java
Compiler” -> “Annotation Processing”, check “Enable project
specific settings” and then click “Apply”.
5. Continue to “Annotation Processing” -> “Factory Path” and
check “Enable project specific settings”. “Click Add JARs” and
select the realm jar file in “libs”, click OK, Apply and finally
build.
6. In order to trigger the annotation processor, you must add the
annotation @RealmClass before every one of your
RealmObject subclasses.
7
8. Example
1. Adding Realm to an Android Studio Project
2. Creating a Realm (Database)
Result is a Realm file called default.realm
If you want to specify your Realm name use RealmCofiguration.Builder.
8
1 | compile ‘io.realm:realm-android:0.84.1’
1 | Realm myRealm = Realm.getInstance(context);
1 | Realm myOtherRealm =
2 | Realm.getInstance(
3 | new RealmConfiguration.Builder(context)
4 | .name(“mydb.realm”)
5 | .build()
6 | );
9. Example (Cont.)
3. Creating a RealmObject (Table)
Any JavaBean can be stored in a Realm once it extends the
RealmObject class.
9
01 | public class Country extends RealmObject {
02 |
03 | private String name;
04 | private int population;
05 |
06 | public Country() {}
07 |
08 | public String getName() {
09 | return name
10 | }
11 |
12 | public void setName(String name) {
13 | this.name = name;
14 | }
15 |
16 | public int getPopulation() {
17 | return population;
18 | }
19 |
20 | public void setPopulation() {
21 | this.population = population;
22 | }
23 |
24 | }
10. Example (Cont.)
4. Creating Transactions
To ensure atomicity and consistency, Realm forces you to
execute all writing operations inside a transaction.
To start a new transaction, use the beginTransaction method.
Similarly, to end the transaction, use the commitTransaction
method.
10
1 | realm.beginTransaction();
2 |Country country = realm.createObject(Country.class); // Create a new object
3 | country.setName(“Cambodia”);
4 | country.setPopulation(“15000000”);
5 |realm.cancelTransaction();
11. Example (Cont.)
5. Writing Queries
In order to create a query, use the where method of the
relevant Realm object and pass the class of the objects you
are interested in. After creating the query, you can fetch all
results using the findAll method, witch returns a
RealmResults object.
11
1 | RealmResults<Country> result = myRealm.where(Country.class).findAll();
2 | For(Country c:result){
3 | Log.d(“results :” , c.getName() + “ - ” c.getPopulation());
4 | }
5 | //Prints Cambodia - 15000000
12. Tools – Realm Browser
Realm Browser is a standalone application that enable
you to read and edit .realm databases.
Note: The Realm Browser is only available on Mac OS X at the
moment.
12