Skip to content

Instantly share code, notes, and snippets.

@congnt24
Created March 24, 2017 03:48
Show Gist options
  • Save congnt24/a89db00b48813d10226afae85cb9fd89 to your computer and use it in GitHub Desktop.
Save congnt24/a89db00b48813d10226afae85cb9fd89 to your computer and use it in GitHub Desktop.
Tips for using realm
1. Using realm.executeTransaction instead realm.beginTransaction and realm.commitTransaction because executeTransaction() automatically handles calling realm.cancelTransaction() in case an exception is thrown.
//Using retrolambda
try(Realm realmInstance = Realm.getDefaultInstance()) {
realmInstance.executeTransaction((realm) -> realm.insertOrUpdate(dog));
}
2. Close Realm after opening Realm instances
3. A tip: you should open your first Realm instance on the UI thread only after the first Activity has been created, just to be safe from context.getFilesDir() == null
4. using realmREsults.asObservable() or Observable.just(realm.where(...)).find*()
5. Using findAllSorted() instead of findAll.sort()
6. Using @Index to speed up
7. GetData INSIDE transaction
try(Realm realm = Realm.getDefaultInstance()) {
realm.executeTransaction(inRealm -> { // YES
final RealmResults<Dog> dogs = inRealm.where(Dog.class).findAll(); // YES
for(Dog dog : dogs) { // YES
//...
}
});
}
8. Using RealmREcyclerViewAdapter and RecyclerView
#reference: Medium, https://github.com/miquelbeltran/android-rxjava-realm-cache/blob/master/app/src/main/java/work/beltran/rxrealmcache/MainActivity.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment