<@U0KBF8D7V> who then subscribes?
# rx
u
@Paul Woitaschek who then subscribes?
u
@ursus for that use case, I would normally return a Disposable from the method. And let the caller decide the lifespan/scope of that subscription. That makes it a lot more manageable imho.
u
sounds reasonable, what about object that needs to relay output of a cold observable to hot one? something like this?
Copy code
public class AppVisibilityManager {

    private final BehaviorRelay<Boolean> inForegroundRelay = BehaviorRelay.create();

    public AppVisibilityManager(Application application) {
        checkNotNull(application);

        application.registerActivityLifecycleCallbacks(new AppVisibilityLifecycleObserver() {
            @Override public void onAppVisibilityChanged(boolean inForeground) {
                inForegroundRelay.accept(inForeground);
            }
        });
    }

    public Observable<Boolean> inForegroundObservable() {
        return inForegroundRelay.distinctUntilChanged();
    }
}
or would you lazy create that cold one and make it hot via operators?
u
I would probably subscribe to the inForegroundObservable() upon Application.onCreate. Or call a
Copy code
fun bind() : Observable<Boolean>
that returns a memoized Observable instance of
Copy code
inForegroundRelay.distinctUntilChanged().share()
(or whatever publish()/replay() variant you need. Not sure how this relates to the kotlin rx channel though?
u
right, but then its up to callee to sub to it to start it, i somehow dont like the coldness in this case ... i dont think rx relates to kotlin anyhow
u
Since the subscription in this case most likely is scoped to the application/process lifespan, you could just subscribe to the observable in the AppVisibilityManager ctor and instantiate it at an appropriate spot, like application.onCreate. But I just find it much more implicit (and more difficult to test) than exposing a bind() method (that should have returned ```Disposable```above, I mistyped)
u
yea you are right, and also you are being subscribe on some random thread which DI chooses since it calls the ctors
which is in practise almost always mainthread but you never know