Realm Java 1.1.0 — New and faster Insert API!
We just released a new version of Realm Java to this website and to Bintray. It contains a new insert API as well as a number of bug fixes.
Insert API
Realm’s storage engine has always been faster than SQLite when it comes to inserting data, but due to a number of design decisions in the Realm Java API, that advantage was mostly lost. This meant that Realm was generally as fast as SQLite for a small number of items (<1000), but started getting slower the more items you added in a batch.
In v1.1.0 we came up with some optimizations and now offer 4 new methods:
void Realm.insert(RealmModel obj)
void Realm.insert(Collection collection)
void Realm.insertOrUpdate(RealmModel obj)
void Realm.insertOrUpdate(Collection collection)
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.insertOrUpdate(restApi.getPersons());
realm.commitTransaction();
These methods differ mostly from the standard Realm.copyToRealm()
in the sense that they do not return any objects. This has allowed us to reduce memory allocations to almost zero as well as remove many checks that were pure overhead.
Doing this, we went from being ~40% slower than an optimized SQLite implementation to being ~70% faster for 100K objects.
We have released our benchmarks repo so you can verify these numbers yourself. Find it here. The numbers above were produced on a Nexus 6P using stock Android 6.0.1.
Note that due to Realm’s auto-update features it is possible to query for data before they are even saved, and you will get notified when the data is available. This can be a huge benefit for those of you that want to separate displaying data from saving it:
final PersonApi api = new PersonApi();
Realm realm = Realm.getDefaultInstance();
RealmResults<Person> persons = realm.where(Person.class).findAllAsync();
person.addChangeListner(new RealmChangeListener() {
@Override
public void onChange(RealmResults<Person> persons) {
if (!persons.isEmpty()) {
// Callback when data is available
}
}
});
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.insertOrUpdate(api.getPersons());
}
});
Bug fixes
-
Combining async transactions and async queries on the UI thread could result in the transaction
onSuccess
callback being invoked before the data is available on the UI thread. -
The
RealmOptionalAPITransformer
introduced in 1.0.1 does not work with DexGuard and has been disabled for now.
See the full changelog for all the details.
Thanks for reading. Now go forth and build amazing apps with Realm! As always, we’re around on Stack Overflow, GitHub, and Twitter.