Skip to main content

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. 

You should always keep a backup of all the data on your Android phone. What to do if your phone is lost or stolen?  How to secure all the important information including the messages, contacts, photos and other data? Stay calm and take a breather. Protect all your data and avoid that disastrous scenario. There are numerous ways to automatically back up and sync your data from your Android device. Five easy steps are listed here to access, share, and manage your files on the go.

1. Backup your data

Automatically backup all your folders from your computer to provide real-time access to your data on your Android device. You can easily restore your files to your Android device whenever you like.

2. Sync your files to your device

Synchronize all your files, photos, music, videos, bookmarks and contacts between Android phones and computer without any effort from Android app developers.

3. Offline access

It is relatively easy to access all your files and documents offline. View and edit your files across your devices and smoothly synchronize your changes in data when the files are turned on when you're back at the office or at home.

4. Secure file sharing

Choose specific team members to view your files and folders instantly from your Android device. Give restricted access to share unlimited number of folders to have complete control over file sharing. Post your files on social media such as Facebook or Twitter.

5. Schedule your file sync

Schedule your synchronization of the files and photos in your collection to run in a specific time to get things done easier and faster on the go.

Subscribe to Android developer
 

start with anubavam today

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

Request Quote