Hey, I am trying to use RxJava throughout the app, I really like the concept, however I struggle with , as blogposts at tech talks make it too simplistic.

First thing that me is what everybody uses as example — Repository exposes a cold observable which is say forexample request observable. Ui/Presenter subscribes, however when it subscribes it cancels the request. This is super bad if you had some flatMaps on the stream which write to database .. Ui goes away and you skip your writing to database, bad.

Okay maybe the layer which outlives presenter should subscribe and relay emits to Relay which it exposes. I.e. there are pair methods, 1. void that subscribes the obs, 2. that exposes the relay. I like this design because it remind me of eventbus, just a passive observer like this:

class @Singleton RestManager 
private final BehaviorRelay<FooEvent> mFooRelay

public void requestFoo() {
        .map(foo -> FooEventSuccess(foo))
        .onErrorReturn(__ -> FooEventError())
        .subscribe( mFooRelay )}

public Observable<FooEvent> fooObservable() {
    return mFooRelay.hide()

This is pretty much what people parrot around here that you should use disc as single source of truth, (which I dont think is always necessary, you then uptake problematics of syncing, purging, invalidating database), but lets say that mFooRelay is our single source of truth, which is good.

However when you subscribe, you jump out of the world. By doing which you loose all of control of streams, i.e. fo rexample requests will be independent. No way (at least to my limited rx knowledge) to manage their races, incoming order. etc. Like in this case.

    .switchMap(sort -> paginationObservable()
        .switchMap(pageIndex -> mRestManager.requestFoo(sort, pageIndex)))
    .subscribe(fooEvent -> ...)

For every new sort setting from ui or new page you need to rerun request. You need that requestFoo() to return Observable in order for it to get unsubscribed when new pageIndex or sort comes. Otherwise its independent requests, were back in before-rx-times. When you unsubscribe the relay it does nothing to its “upstream” which it proxies.
Or the posterbody rx example edittext -> switchmap apirequest per letter.

So maybe its okay for some observables to be ui bound, for some its not.

This asymmetry bothers me. I dont see anyone talking about it.

When its ui bound you need to restore the state manually, retained object, presenter, parcelable whatever. When its not you dont need to. Now I have some presenters retained, some not. Idk, feels weird, im frustrated

Source link


Please enter your comment!
Please enter your name here