-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Design] RxJava support #1651
[Design] RxJava support #1651
Conversation
I'm certainly against C option to include RxJava. While it might be easiest way to do it, libraries like Retrofit have moved towards hybrid A/B approach exactly because it's the worst way to do it too. I see it similar to Retrofit, there's an "adapter" addon which turns findAll() and similar methods to return Observable and it has to be registered on configuration step. And given the fact that Realm instances can be configured when calling getInstance(), programmer can temporarily enable/disable it too by simply providing/not providing adapter. Also, it would be awesome for Observable to emit onNext with updated results every time Realm changes. Or even better, when specific RealmResults change. Should these Observables also be deferred? I.e. wait until at least one subscriber? |
The Adapter approach is really cool. Unfortunately it works for Retrofit because the user defines the interfaces, unlike Realm where we provide them. Regarding onNext, then that is exactly what we intend to do. This is already happening today because RealmObjects auto-refresh themselves, so it is just a matter of putting RxJava semantics on top of our own ChangeListeners. As I mention in the docs, then we can only defer work if you observe on a RealmQuery (which brings a few more headaches regarding our different query options). Observables created from e.g. RealmResults would already have done the work as it is similar to observing on a List. |
@cmelchior Ah, I see, so unless RealmQuery is used, it's practically just async query wrapped in Observable? As for adapter approach, I was just telling my thoughts. I kinda figured it's not directly applicable. In any case C approach would introduce more coupling and I'm on "opt-in" side instead of "opt-out" even though I always use RxJava on Android and it's not a big deal for me personally. |
@fuwaneko Yes, that is correct. I totally agree with your point about case C. It is pretty much a choice between "less coupling and slightly worse API" or "stronger coupling and better API". |
@cmelchior haha, in that case I vote for less coupling and B version. In any case, thanks for working on adding this. |
Would go for option B as well. Better keep it open to allow adding support for future libs that may come up. |
Why not go with simple Futures, so RxJava is not baked into the codebase yet still usable. Option A is fine for me as long as you don't branch to RxRealm. |
We had some talks internally about this, and by the looks of it we will probably go with some variant of proposal B. It has been summarized in the top post. |
I like the option B as well. Will be pretty straightforward to use. But I do not think that RxJava support should be added today even if it can be. Threading and notification issues are preferred to be resolved before imo. This won't confuse users and lead them to inappropriate usage scenarios. Would be good if RxJava support will be in 1.0 release. And if it do not it will be still better than unfinished RxJava compatible version. Anyway 👍 for making this. |
Support has been released as part of 0.87. Closing |
What about RxJava 2 support? |
@tonyshkurenko Please create a new issue for RxJava 2 support. Thanks in advance. |
This PR contains a document describing how we could go about adding RxJava support to Realm.
See #865 for the issue and https://github.com/realm/realm-java/tree/cm-lab-rxjava for some earlier prototype code.
Update 26/10-2015
After some discussion, we are narrowing down to Option B:
.observable()
support for all Realm classes.Update 07/12-2015
Basic API (v1)
Realm.asObservable()
RealmObject.asObservable()
RealmResults.asObservable()
DynamicRealm.asObservable()
DynamicRealmObject.asObservable()
RealmList.asObservable()
(not yet supported)Using
asObservable()
instead ofobservable()
. It will be consistent with the wording used by e.g Subjects and will fit well when we addasCursor()
( #1090 )RxObservableFactory
RealmObservabableFactory
for automatically converting RealmChangeListener to Observables (default behavior).RealmConfiguration.Builder.rxFactory(RxObservableFactory)
Advanced API (v2):
RealmQuery.asObservable()
DetachedCopyObservableFactory
variant that automatically usesRealm.copyFromRealm
to copy RealmObject/RealmResults to memory. Ideally it should be possible to control which thread does the copying which require exposing object/RealmResult handover.Implementation notes:
Realm.copyFromRealm
can provide a temporary work-around, but is not without cost itself.Examples:
@realm/java