RxJava support
I'm not sure if the Realm internals allow for RxJava support, but it would be nice to be able to do something like this:
// Get observable of all users
Realm.getInstance(context)
.where(User.class)
.findAll()
.observable()
.subscribe(users -> {
// do stuff with users
});
// Get observable of a single user
Realm.getInstance(context)
.where(User.class)
.findFirst()
.observable()
.subscribe(user -> {
// do stuff with user
});
// Get observable of users table count
Realm.getInstance(context)
.where(User.class)
.findAll()
.size()
.observable()
.subscribe(count -> {
// do stuff with count
});
+1
We definitely have RxJava on the radar, as it quite a cool framework. A first step for us is probably documenting what works and what doesn't when combining Realm and RxJava, but going forward adding some sort of support is not out of the question.
That said we are currently focusing on other features right now, so I cannot give you any timeframe just yet.
+1
Some resources:
- https://github.com/ReactiveX/RxJava/wiki
- http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/
- https://www.youtube.com/watch?v=_t06LRX0DV0
- http://mttkay.github.io/blog/2013/08/25/functional-reactive-programming-on-android-with-rxjava/
POC with thoughts:
- https://github.com/realm/realm-java/tree/cm-lab-rxjava
+1
Internal RxJava support would be great. For now I'm using it with Realm by following several restrictions:
- One observable for one transaction.
- Mapping Realm objects to non-Realm (e.g. AutoValue) when passing between observer/subscriber threads.
Hi @kboyarshinov Can you elaborate a bit on one observable pr. transaction?
I have been evaluating RxJava support as well, and the biggest blockers I can see so far is that we need fine-grained notifications to observe on queries and that it is not possible to move RealmObjects between threads which conflict with Observable.subscribeOn and Observerable.oberserveOn().
The second part is probably the biggest issue.
@cmelchior I implemented OnSubscribeRealm function which controls creation and closing of Realm instance following Observable contract. And it defines custom function to manipulate data with that instance in a single transaction.
One observable per transaction means every RealmObservable operates its own instance of Realm and executes used defined function with this instance in isolated transaction.
Also there is a RealmObservable helper class - it simplifies usage of OnSubscribe function. Full example is also available.
+1
+1
+1!
+1!!!
+1
+1
+1
+1 too
+1
+1
+1
+1
+1