Skip to main content
Anbarasu

Working with Realm Mobile Database in Android App Projects


Realm for Android

Realm data is compatible with Android mobile devices. It uses very little disk space, realm file perform better than SQLite or JSON. Here are the features of Realm database:

Features

  • Realm is light weight database designed for mobile devices.
  • Replacement for SQLITE in mobile devices
  • It is a kind of No-SQL database
  • Fully Object Oriented.
  • Mobile-first written in C++
  • Cross platform support

Given below are the advantages of Realm 

  • 10X faster than SQLite
  • Easy to use - no need of query knowledge
  • Auto casting as a particular type

Given below are some of the disadvantages of using Realm

  • Still under development. However, Android app developers can proceed with released stable versions
  • Concurrent issue not handled    
  • No null support
  • No auto increment IDs
  • No mapping support - no foreign key
  • Migration is difficult

Integrating data in Realm

  1. Configuration
  2. Initialization
  3. CRUD operations
  1. Configuration
    • Single Module support
      • add the following to your app build.gradle file
      • compile 'io.realm:realm-android:2.3.1’ in your dependency
    • Multiple module support
      • add the following to your Project build.gradle file
        classpath "io.realm:realm-gradle-plugin:2.3.1" in your dependency
      • add the following to your app build.gradle file
        apply plugin: 'realm-android'

    thats it RealM configured in your project.

  2. Initialization
    • Init the Realm in your Application class and its custom configuration
      public class MyApplication extends Application {
       
            @Override
            public void onCreate() {
       
            super.onCreate();
      
            Realm.init(this);
                      
            RealmConfiguration realmConfiguration = new                             
                               RealmConfiguration.Builder(this)
                              .name(Realm.DEFAULT_REALM_NAME)
                              .schemaVersion(0)
                              .deleteRealmIfMigrationNeeded()
                              .build();
           Realm.setDefaultConfiguration(realmConfiguration);
       
                  }
      }
    • If you want can add database encryption by using application keystore file.
  3. CRUD Operations.
    1. A class can act as a table - query is not needed.
      Class Dog extends RealmObject{
      
              @PrimaryKey
              String key = "abc";
                  or
              int key = 123;
      
              @Ignore
                  private int sessionId;
      
              }
      
      • All the fields inside the class act as a column.
      • The field with @PrimaryKey annotation act as primary key for the table.
      • The field with @Ignore annotation can not be considered as a column - this filed ignored and no column is created for the filed
    2. Creating Entry.
      1. prepare UNMANAGED entries - does not having default entry in the table.
        Dog dog = new Dog();
        dog.setName("Rex");
        dog.setAge(1);
        
      2. prepare MANAGED entries - can have its default entry in the table
        Dog dog = realm.createObject(Dog.class);;
        dog.setName("Rex");
        dog.setAge(1);
        
    3. inserting into db
      • Get a Realm instance for this thread
        Realm realm = Realm.getDefaultInstance();
        
      • Before doing any write operation we must call
        realm.beginTransaction();
        
      • Do your write operation after beginTransaction.
        // Persist unmanaged objects
        final Dog managedDog = realm.copyToRealm(dog);
        
        // Create managed objects directly
        Person person = realm.createObject(Person.class);
        person.getDogs().add(managedDog);
        
      • After your write operation commit your updates into corresponding table
        realm.commitTransaction();
        

Examples :

  • Write operation
    Dog dog = new Dog();
    dog.setName("Rex");
    dog.setAge(1);
    realm.beginTransaction();
    final Dog insertedRow = realm.copyToRealm(dog); 
    realm.commitTransaction();
    
  • Update operation
    dog.setName(“another name”);
    realm.beginTransaction();
    final Dog insertedRow = realm.copyToRealmOrUpdate(dog); 
    realm.commitTransaction();
    
  • Read Operation
    realm.where(Book.class).findAll();
    
  • Read Operation with conditions
    realm.where(Book.class).equalTo("id", id).findFirst();
    realm.where(Book.class)
                    .contains("author", "an")
                    .or()
                    .contains("title", "re")
                    .findAll();
    
  • Delete Operation
    // remove a single object
        Dog dog;
        dog.deleteFromRealm();
    
  • Truncate operation
    realm.beginTransaction();
    realm.clear(Book.class);
    realm.commitTransaction();
    

Got an Idea of Android App Development? What are you still waiting for? Contact us now and see the idea live soon. 

Comments

Leave Your Comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
 

start with anubavam today

You have an idea we have engineers to convert your ideas into reality

Request Quote