Realm is a replacement for SQLite and CoreData databases that allows for faster access and easier object mapping. It is a cross-platform mobile database that can provide up to 10x faster performance than raw SQLite. Realm uses a simple object-oriented model and allows for complex queries and relationships between objects. It also supports asynchronous access and notifications when data changes.
1. Realm Java for Android
Gokhan Arik
Android Developer at Client Resources Inc.
www.gokhanarik.com
2. What is Realm?
Replacement database for SQLite and
CoreData
Cross platform mobile database
Founded by Alexander Stigsen and Bjarne
Christiansen (former Nokia emp.)
Known as Tight DB in the past
100 million devices in 9 months
8. That’s why
Faster than SQLite (up to 10x speed up over raw SQLite)
Please check out this article about Kevin Galligan’s concerns about the
performance of Realm
http://kpgalligan.tumblr.com/post/133281929963/my-
talk-at-droidcon-uk
14. Like this (as of version 1.0.0)
No Maven or Ant support
Supports Android since API Level 9 (Android 2.3 Gingerbread & above)
As of version 1.0.0 Eclipse is not supported
No need Proguard configuration. It is included in library
15. Like this (as of version 1.0.0)
Step 1: Add the following class path dependency to the project level build.gradle file.
Step 2: Apply the realm-android plugin to the top of application level build.gradle file
42. Final Notes
perform all Realm write operations on a background thread (not Android’s main thread)
RealmObjects and RealmResults access all the data they refer to lazily. For this reason it is
important to keep the Realm instance open for as long as you want to access your Realm
objects or query results
Database that runs in the phone
Memory efficient. Using C++
Released in July 2014
iOS 2 weeks 200,000 developers
YCombinator
More than 1 billion devices
1 out of 10 apps
Wrong
Pick ORM for feature, not speed
I will go into details of changesets. Many of you probably started working with Realm earlier
The annotation @Ignore implies that a field should not be persisted to disk.
A Realm model class also supports public, protected and private fields as well as custom methods.
Custom methods since 0.88
You can let the fields be public, and use simple assignments instead of setters and getter
No need for final, transient and volatile
Realm model classes are not allowed to extend any other object than RealmObject
The annotation @Index will add a search index to the field.
@PrimaryKey implicitly sets the annotation @Index
Some JSON APIs will return arrays of primitive types like integers or Strings, which Realm doesn’t support yet.
boolean, byte, short, ìnt, long, float, double, String, Date and byte[].
The integer types byte, short, int, and long are all mapped to the same type (long actually) within Realm.
Boxed types like String, Date, Integer and Double are now nullable by default as of 0.83
RealmObjects are live, auto-updating views into the underlying data, which means objects never have to be refreshed. Modifying objects that affect the query will be reflected in the results immediately.
This property of all RealmObject’s and RealmResults not only keeps Realm fast and efficient, but it allows your code to be simpler and more reactive. For example, if your Activity or Fragment is dependent on a specific RealmObject or RealmResults instance, you don’t need worry about refreshing or re-fetching it before updating the UI.
When calling copyToRealmOrUpdate() on classes without primary keys, an exception will be thrown.
Make sure it extends RealmObject unless you @Ignore it
You could have RealmList<Contact> friends;
Read operations are implicit which means that objects can be accessed and queried at any time. All write operations (adding, modifying, and removing objects) must be wrapped in write transactions.
You can commit or cancel
It will automatically handle begin/commit, and cancel if an error happens.
executeTransactionAsync introduced in 0.84
OnSuccess and OnError callbacks are both optional, but if provided they will be called when the transaction completes successfully or fails, respectively.
This object can be used to cancel any pending transaction if you are quitting the Activity/Fragment before the transaction is completed. Forgetting to cancel a transaction can crash the app if the callback updates the UI.
The RealmResults inherits from Java’s AbstractList, and behaves in similar ways.
When a query does not have any matches, the returned RealmResults object will not be null, but the size() method will return 0.
Sort enum introduced in 0.86
Sort enum introduced in 0.86
Async Queries introduced in 0.84
findAllAsync() instead of findAll()
Async Queries introduced in 0.84
findAllAsync() instead of findAll()
Realm instances are reference counted, which means that if you call getInstance() twice in a thread, you will also have to call close() twice as well.
The AsyncTask class contains the doInBackground() method which executes a background thread. The IntentService class contains the onHandleIntent(Intent intent) method which executes in a worker thread.
If you need to use Realm in either of these methods you should open the Realm, perform your work and then close the Ream before exiting. Below are a couple of examples.
The AsyncTask class contains the doInBackground() method which executes a background thread. The IntentService class contains the onHandleIntent(Intent intent) method which executes in a worker thread.
If you need to use Realm in either of these methods you should open the Realm, perform your work and then close the Ream before exiting. Below are a couple of examples.
The AsyncTask class contains the doInBackground() method which executes a background thread. The IntentService class contains the onHandleIntent(Intent intent) method which executes in a worker thread.
If you need to use Realm in either of these methods you should open the Realm, perform your work and then close the Ream before exiting. Below are a couple of examples.