RxJava support for executeTransaction
It would be nice to have an async executeTransaction method in Realm-class which returns an Observable since we RxJava-users don't like callables ;) Maybe something like:
Observable<void> executeTransaction(Realm.Transaction transaction)
Usage would be:
Observable<void> transaction = realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
User user = bgRealm.createObject(User.class);
user.setName("John");
user.setEmail("[email protected]");
}
});
transaction.subscribe(new rx.Subscriber<T> {
@Override public void onCompleted() {
//do your stuff here, if transaction was successful
}
@Override public void onError(Throwable e) {
//cleanup
}
@Override public void onNext(T t) {
//nothing.
}
});
Yes, that is a good idea and something we have been considering ourselves.
Copy from #2272:
Background
The first class support for RxJava in Realm is great, however that said I couldn't find a way to make writes work with the current API. As a result using callbacks rather than reactive seemed to be the sensible choice with the current setup.
Proposal
The Completion or Single APIs of RxJava map well to the Callback object created as part of the asynchronous write transaction. Could the callback be abstracted or replace with one of the aforementioned types of Observable?
The Single API defines the same methods as the Callback. This is documented here http://reactivex.io/documentation/single.html
The process would only emit one item therefore the use of Single seems like a logical fit.
Any update on this? I would love to help with this. Just not sure what the current status is.
Hi @jophde
We are currently focusing on other issues, so would gladly accept any PR's for this. The current tasks for this should be:
- [ ] Add support for
Realm.asObservable(Transaction) - [ ] Add support for
DynamicRealm.asObservable(Transaction) - [ ] Extend the RxObservableFactory interface with two new methods:
.from(Realm realm, Realm.Transaction).from(DynamicRealm realm, DynamicRealm.Transaction)
- [ ] Implement functionality in
RealmObserverableFactory. - [ ] Add unit tests to
RxJavaTests.
All implementation should reside inside the RealmObservableFactory class. Ideally it should be a one-shot operation that just executes the transaction them completes. Ideally using defer.
While Completable is theoretically the more correct to return it is still so new that we would rather let people convert it to that themselves (also it still has bugs).
+1
+1
+1
+1
Copied from #3213
@cvoronin wrote,
What
I would like to have ability to get result of a query not only as Observable, but also as Single
Why
Our Retrofit API often returns results as Single. If I had the ability to get query results as Single, it were possible to use those results in concat:
Single
.concat(getFromRealm(), getFromServer())
.first()
.subscribe(...)
Now I have to use something like this:
fun loadAll(): Single<List<Item>> {
return Single.just(
with(realmProvider.get()) {
use { realm ->
realm.copyFromRealm( realm.where(Item::class.java).findAll() )
}
}
)
}
+1
+1
Best thing about this is that Observable<Void> won't be a valid thing with RxJava2 because only possible value for Void is null, and RxJava2's contract (the Reactive-Streams specification) says that Observable.just(null) is an immediately thrown NPE.
Are you sure about this on rxjava2? That is a big change then. As other example onErrorResumeNext() wont be available or a backport should be available, as avoiding errors you need Observable.just(null) on RxJava2 Thanks didn't knew about that fact
RxJava has Completable which should be used for cases like this. It will either complete or error.
RxJava2 is indeed incompatible with RxJava1, which means we also need to look at how to support both versions going forward. This is covered by #3497
+1
+1
Personally I think this is not realm's responsibility, if people need an Rx wrapper around Realm's methods (even for things that are technically just synchronous method call), then it's up to them, really.