These are chat archives for ReactiveX/RxJava

Sep 2016
Toon Sevrin
Sep 13 2016 16:30
@Dorus Nooooo ReactiveX/RxJava#4545
Sep 13 2016 18:44

Can anyone suggest a more elegant way of doing the following:

  • I have a mixin class that monitors bootstrap and authentication states for Activity and Fragment. The mixin then emits an authenticated session into a BehaviorSubject, which can be accessed via a public method.
  • Once the session becomes active, an API client can be requested using the session.
  • Within a fragment, I create a BehaviorSubject and emit the client every time I acquire it.
  • The fragment itself is responsible for periodically fetching a list of entries from the client.
  • The fetching is setup in onResume and demolished in onPause

Rough code:

public class MyFrag extends RxFragment {
    private AccountMixin mAccountMixin = null;
    private AuthenticatedSession mSession = null;
    protected ApiClient mClient = null;
    private BehaviorSubject<ApiClient> mClientSubject = null;

    public void onCreate(Bundle savedInstanceState) {

        mClientSubject = BehaviorSubject.create();

    public void onStart() {

                .compose(RxLifecycle.bindUntilEvent(lifecycle(), FragmentEvent.STOP))

    private void onSession(final AuthenticatedSession session) {
        mSession = session;
        mClient = ApiProvider.requestClient(session);

    public void onResume() {

        final Observable<List<EntryInfo>> entryObservable = Observable
                .interval(5, TimeUnit.SECONDS)
                .switchMap(tick -> mClient.getEntries())

                .filter(client -> client != null)
                .switchMap(c -> entryObservable)
                .compose(RxLifecycle.bindUntilEvent(lifecycle(), FragmentEvent.PAUSE))

Obviously, the code is a mess. However, I cannot see how to make it more elegant. The AccountSession can be emitted at any point in time (e.g., during Fragment's start, resume, pause, or stop events). However, I need to start list initial load and refresh only during onResume state. I feel like I'm just approaching this thing from the wrong angle. Any suggestions?

Sep 13 2016 18:46
@toonsevrin Nice. I guess we should have checked the latest snapshot too :)
Sep 13 2016 23:35
This message was deleted