Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Realm database

507 views

Published on

This presentation describe about Realm foundation.

Published in: Mobile
  • Be the first to comment

  • Be the first to like this

Realm database

  1. 1. Database Pen Voneat 2016.01.14 1
  2. 2. Contents I. Introduction II. Installation III. Syntax IV. Example V. Tools VI. References 2
  3. 3. Realm is… A mobile database that comes to replace SQLite & Core Data. Why? 1. Simple (MVCC model) 2. Cross-Platform (ios & Android) 3. Faster (zero-copy architecture) 3
  4. 4. Comparison 4
  5. 5. Prerequisites  Android Studio >= 0.8.6  JDK version >=7  API Level 9+ (Android 2.3+) 5
  6. 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. 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. 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. 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. 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. 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. 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
  13. 13. References  https://realm.io/docs/java/  http://code.tutsplus.com/tutorials/up-and-running- with-realm-for-android--cms-25241 13

×