Parinda Rajapaksha
What is Realm?
➔ Object Database Management System
➔ Supports for,
- Android
- Objective C
- Swift
- Xamarin
- React Native
Over 2 billion users
Realm Mobile Database
➔ Alternative for
- SQLite
- Core Data, OrmLite
➔ Features
- Offline -first functionality
- Fast queries
- Cross platform
Getting Started
➔ Prerequisite
- JDK, SDK
- Android Studio , Eclipse
- Android 2.3 and above
- Gradle , Ant, Maven
➔ Installation
dependencies {
Project level
Application level
➔ Initialization
Realm.init(context);
➔ Models
public class User extends RealmObject {
@Required
private String name;
@PrimaryKey
private int nic;
@Ignore
private int sessionId;
}
Relationships
➔ Many to Many
public class Contact extends RealmObject {
public String name;
public RealmList<Email> emails;
}
➔ Inverse relationships
- Unidirectional
public class Person extends RealmObject {
private String id;
private String name;
private RealmList<Dog> dogs;
}
public class Dog extends RealmObject {
private String id;
private String name;
@LinkingObjects("dogs")
private final RealmResults<Person> owners;
}
➔ Creating Objects
realm.beginTransaction();
User user = realm.createObject(User.class);
user.setName("John");
realm.commitTransaction();
User user = new User("John");
realm.beginTransaction();
User realmUser = realm.copyToRealm(user);
realm.commitTransaction();
Writes
➔ Asynchronous Transactions
- Avoid blocking UI thread
- Execute, onSuccess, onError
➔ Queries
RealmResults<Person> teenagers = realm.where(Person.class)
.between("age", 13, 20)
.findAll();
➔ In-Memory Realms
- Un Persisted
- Deleted when realm is closed
- Memory Low??
- Same name??
➔ Dynamic Realms
- Type safety, flexibility
DynamicRealmObject person = realm.createObject("Person");
➔ No support for final and volatile fields
➔ Model classes not allowed to extend any other objects
➔ Realm objects cannot be moved across threads
➔ No auto increment primary keys
➔ Not possible to access the database from distinct
processes at same time
Limitations
➔ Ease of use
public class Product extends RealmObject{
@PrimaryKey
private long id;
private String productName;
}
User user = realm.createObject(Product.class);
private static final String CREATE_PRODUCT_TABLE = "CREATE TABLE " +
Constants.PRODUCT_TABLE + "(" + Constants.COLUMN_ID + " INTEGER PRIMARY
KEY AUTOINCREMENT," + Constants.COLUMN_NAME + " TEXT NOT NULL,” + ")";
Realm DB
SQLite
Realm over SQLite ?
➔ Ease of use
➔ Cross platform
➔ Realm browser for ios
➔ Json support
➔ Encryption support
➔ Data change notification
Realm over SQLite ?
➔ Realm browser for ios
➔ Ease of use
➔ Cross platform
➔ Realm browser for ios
➔ Json support
➔ Encryption support
➔ Data change notification
Realm over SQLite ?
Performance
➔ Preventing Application Not Responding Errors
- Realm write operations on a background thread
➔ Reuse RealmResults and RealmObjects
Best Practices
Thank You

Realm mobile database

  • 1.
  • 2.
    What is Realm? ➔Object Database Management System ➔ Supports for, - Android - Objective C - Swift - Xamarin - React Native
  • 3.
  • 4.
    Realm Mobile Database ➔Alternative for - SQLite - Core Data, OrmLite ➔ Features - Offline -first functionality - Fast queries - Cross platform
  • 5.
    Getting Started ➔ Prerequisite -JDK, SDK - Android Studio , Eclipse - Android 2.3 and above - Gradle , Ant, Maven ➔ Installation dependencies { Project level Application level
  • 6.
    ➔ Initialization Realm.init(context); ➔ Models publicclass User extends RealmObject { @Required private String name; @PrimaryKey private int nic; @Ignore private int sessionId; }
  • 7.
    Relationships ➔ Many toMany public class Contact extends RealmObject { public String name; public RealmList<Email> emails; } ➔ Inverse relationships - Unidirectional
  • 8.
    public class Personextends RealmObject { private String id; private String name; private RealmList<Dog> dogs; } public class Dog extends RealmObject { private String id; private String name; @LinkingObjects("dogs") private final RealmResults<Person> owners; }
  • 9.
    ➔ Creating Objects realm.beginTransaction(); Useruser = realm.createObject(User.class); user.setName("John"); realm.commitTransaction(); User user = new User("John"); realm.beginTransaction(); User realmUser = realm.copyToRealm(user); realm.commitTransaction(); Writes
  • 10.
    ➔ Asynchronous Transactions -Avoid blocking UI thread - Execute, onSuccess, onError ➔ Queries RealmResults<Person> teenagers = realm.where(Person.class) .between("age", 13, 20) .findAll();
  • 11.
    ➔ In-Memory Realms -Un Persisted - Deleted when realm is closed - Memory Low?? - Same name?? ➔ Dynamic Realms - Type safety, flexibility DynamicRealmObject person = realm.createObject("Person");
  • 12.
    ➔ No supportfor final and volatile fields ➔ Model classes not allowed to extend any other objects ➔ Realm objects cannot be moved across threads ➔ No auto increment primary keys ➔ Not possible to access the database from distinct processes at same time Limitations
  • 13.
    ➔ Ease ofuse public class Product extends RealmObject{ @PrimaryKey private long id; private String productName; } User user = realm.createObject(Product.class); private static final String CREATE_PRODUCT_TABLE = "CREATE TABLE " + Constants.PRODUCT_TABLE + "(" + Constants.COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + Constants.COLUMN_NAME + " TEXT NOT NULL,” + ")"; Realm DB SQLite Realm over SQLite ?
  • 14.
    ➔ Ease ofuse ➔ Cross platform ➔ Realm browser for ios ➔ Json support ➔ Encryption support ➔ Data change notification Realm over SQLite ?
  • 15.
  • 16.
    ➔ Ease ofuse ➔ Cross platform ➔ Realm browser for ios ➔ Json support ➔ Encryption support ➔ Data change notification Realm over SQLite ?
  • 17.
  • 20.
    ➔ Preventing ApplicationNot Responding Errors - Realm write operations on a background thread ➔ Reuse RealmResults and RealmObjects Best Practices
  • 21.

Editor's Notes

  • #3 Realm is an open-source object database management system, initially for mobile (Android/iOS),[1] also available for platforms such as Xamarin[2] or React Native,[3] and others,[4][5]including desktop applications (Windows[6]), and is licensed under the Apache License. Announces 2016 but initialy release in january 2017. Mobile platform : A flexible platform for creating offline-first, reactive mobile apps effortlessly.
  • #4 As of today Over 2 billion users rely on Realm It is trusted by some well known companies like
  • #5 realm is not using SQLite as it’s engine. Instead it has own C++ core and aims to provide a mobile-first alternative to SQLite. Make your app work as well offline as it does online. Data is stored locally in the Realm Mobile Database, which means that each device can fully function when offline, then re-sync when the network comes back. The Realm Mobile Platform handles the complexities of network state and conflict management, so you never have to worry about server errors Even complex queries take nanoseconds, and stay up to date with new data. Use the same database for all your apps, on any major platform. Secure your data with transparent encryption and decryption. Keep your users’ data safe with AES-256 encryption, strong enough for global financial institutions. Connect your UI to Realm, and data changes will appear automatically. Realm objects are always live, which means that they always have the latest data. Your app will never be out of date again. Realms are zero-copy: data is not copied in and out of the database to be accessed; you’re working with the objects directly.
  • #6 JDK version 7.0 or higher Android Studio version 1.5.1 or higher. We no longer support Eclipse as an IDE; please migrate to Android Studio. A recent version of the Android SDK Android API Level 9 or higher (Android 2.3 and above) . # Add the class path dependency to the project level build.gradle file. # Apply the realm-android plugin to the top of the application level build.gradle file. # The Maven & Ant build systems are not supported
  • #7 Realm model classes are created by extending the RealmObject base class. A Realm model class also supports public, protected and private fields as well as custom methods. Realm supports the following field types: boolean, byte, short, int, long, float Using primary keys makes it possible to use the copyToRealmOrUpdate() method, which will look for an existing object with this primary key, and update it if one is found; if none is found, it will create a new object instead. When calling copyToRealmOrUpdate() on classes without primary keys, an exception will be thrown.
  • #8 Relationships are unidirectional. Taking the two classes Person and Dog as an example, it means that you can follow the link from a Person object to the Dog objects but there are no way that you can go from a Dogobject to its Person objects.
  • #11 As transactions are blocked by other transactions it can be an advantage to do all writes on a background thread in order to avoid blocking the UI thread. By using an asynchronous transaction, Realm will run that transaction on a background thread and report back when the transaction is done.
  • #12 Define an instance for an un-persisted in-memory Realm this will create an in-memory Realm instead of saving it to disk. In-memory Realms might still use disk space if memory is running low, but all files created by an in-memory Realm will be deleted when the Realm is closed. Please note that creating an in-memory Realm with the same name as a regular (persisted) Realm is not allowed in some cases these types are not available at compile time e.g., during migrations or when working with string based data like CSV files.
  • #13 Currently, Realm models have no support for final and volatile fields. This is mainly to avoid discrepancies between how an object would behave as managed by Realm or unmanaged. Realm model classes are not allowed to extend any other object than RealmObject. If declared, the default constructor (constructor with no parameters) must always be empty. The reason is that a default contructor will call methods which assume a Realm instance is present. No inheritance Although it’s possible to have multiple threads reading from and writing to the database at the same time, Realm objects cannot be moved across threads. So if, for example, you retrieve a realm object using AsyncTask’s doInBackground(), which runs in a background thread, you cannot pass this instance to the onPostExecute() methods, since those run on the main thread. Possible workarounds for this situation would be to either make a copy of the object and pass it along or pass the object’s id and retrieve the object again on onPostExecute(). It’s not possible to access the database from distinct processes at the same time. According to their documentation, multi-process support is coming soon.
  • #17 Currently, Realm models have no support for final and volatile fields. This is mainly to avoid discrepancies between how an object would behave as managed by Realm or unmanaged. Realm model classes are not allowed to extend any other object than RealmObject. If declared, the default constructor (constructor with no parameters) must always be empty. The reason is that a default contructor will call methods which assume a Realm instance is present. No inheritance Although it’s possible to have multiple threads reading from and writing to the database at the same time, Realm objects cannot be moved across threads. So if, for example, you retrieve a realm object using AsyncTask’s doInBackground(), which runs in a background thread, you cannot pass this instance to the onPostExecute() methods, since those run on the main thread. Possible workarounds for this situation would be to either make a copy of the object and pass it along or pass the object’s id and retrieve the object again on onPostExecute(). It’s not possible to access the database from distinct processes at the same time. According to their documentation, multi-process support is coming soon.
  • #21 ypically Realm is fast enough to read and write data on Android’s main thread. However, write transactions are blocking across threads so in order to prevent accidental ANR’s we advise that you perform all Realm write operations on a background thread (not Android’s main thread) RealmObjects and RealmResults are automatically refreshed when changes are made to the Realm. This means that it isn’t necessary to fetch those objects again when reacting to a RealmChangedListener