7. Relationships
➔ Many to Many
public class Contact extends RealmObject {
public String name;
public RealmList<Email> emails;
}
➔ Inverse relationships
- Unidirectional
8. 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;
}
9. ➔ 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
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 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
13. ➔ 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 ?
14. ➔ Ease of use
➔ Cross platform
➔ Realm browser for ios
➔ Json support
➔ Encryption support
➔ Data change notification
Realm over SQLite ?
20. ➔ Preventing Application Not Responding Errors
- Realm write operations on a background thread
➔ Reuse RealmResults and RealmObjects
Best Practices
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.
As of today Over 2 billion users rely on Realm
It is trusted by some well known companies like
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.
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
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.
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.
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.
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.
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.
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.
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