realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

RxJava support for executeTransaction

Open vtuan10 opened this issue 10 years ago • 17 comments

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.
            }
        });

vtuan10 avatar Jan 21 '16 07:01 vtuan10

Yes, that is a good idea and something we have been considering ourselves.

cmelchior avatar Jan 21 '16 08:01 cmelchior

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.

cmelchior avatar Feb 12 '16 14:02 cmelchior

Any update on this? I would love to help with this. Just not sure what the current status is.

jophde avatar Mar 13 '16 04:03 jophde

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).

cmelchior avatar Mar 15 '16 14:03 cmelchior

+1

philipuvarov avatar Apr 21 '16 12:04 philipuvarov

+1

luispereira avatar Jun 03 '16 09:06 luispereira

+1

aleien avatar Jun 17 '16 15:06 aleien

+1

luispereira avatar Jun 17 '16 15:06 luispereira

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() )
                    }
                }
        )
    }

stk1m1 avatar Jul 26 '16 12:07 stk1m1

+1

JulienDev avatar Oct 23 '16 20:10 JulienDev

+1

alextcn avatar Nov 06 '16 17:11 alextcn

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.

Zhuinden avatar Nov 06 '16 18:11 Zhuinden

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

luispereira avatar Nov 07 '16 08:11 luispereira

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

cmelchior avatar Nov 07 '16 08:11 cmelchior

+1

RicardoNunoAlmeida avatar Jul 24 '18 17:07 RicardoNunoAlmeida

+1

Sunbreak avatar Nov 15 '18 15:11 Sunbreak

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.

Zhuinden avatar Nov 15 '18 15:11 Zhuinden