Last active
August 11, 2016 11:03
-
-
Save Zhuinden/6767238594603b0d9d047795ecc23b4a to your computer and use it in GitHub Desktop.
Realm RxJava write transaction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
catService.getCats() | |
.subscribeOn(Schedulers.io()) | |
.retry() | |
.subscribe(catsBO -> { | |
try(Realm realm = Realm.getDefaultInstance()) { | |
Cat defaultCat = new Cat(); | |
long rank; | |
if(realm.where(Cat.class).count() > 0) { | |
rank = realm.where(Cat.class).max(CatFields.RANK).longValue(); | |
} else { | |
rank = 0; | |
} | |
for(CatBO catBO : catsBO.getCats()) { | |
defaultCat.setId(catBO.getId()); | |
defaultCat.setRank(++rank); | |
defaultCat.setUrl(catBO.getUrl()); | |
realm.insertOrUpdate(defaultCat); | |
} | |
} // auto-close because of try-with-resources | |
}, throwable -> { | |
Log.e(TAG, "An error occurred", throwable); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment