ImmediateLooperScheduler

Additional

Language
Java
Version
v2.0.0 (Nov 21, 2016)
Created
Jun 27, 2016
Updated
Nov 21, 2016 (Retired)
Owner
Masatoshi Kubode (kubode)
Contributor
Masatoshi Kubode (kubode)
1
Activity
Badge
Generate
Download
Source code
APK file

Advertisement

ImmediateLooperScheduler

A Scheduler which executes actions on Looper. It executes actions immediately if it subscribed on same Looper.

Usage

Add dependency to build.gradle.

repositories {
    jcenter()
}
dependencies {
    compile "com.github.kubode:immediate-looper-scheduler:$latestVersion"
}

There are two ways of implementing ImmediateLooperScheduler.

  1. Override AndroidSchedulers.mainThread() with RxAndroidPlugins.

    public class MyApplication extends Application {
        @Override
        protected void onCreate() {
            RxAndroidPlugins.setInitMainThreadSchedulerHandler(schedulerCallable -> ImmediateLooperScheduler.MAIN);
        }
    }
  2. Replace AndroidSchedulers.mainThread() to ImmediateLooperScheduler.MAIN.

    observable.observeOn(ImmediateLooperScheduler.MAIN).subscribe(::doSomething);

Why

AndroidSchedulers.mainThread() is always dispatches actions to Handler.

So, AndroidSchedulers.mainThread() not executes actions when subscribing to data sets that using BehaviorSubject in MVVM pattern.

public static final BehaviorSubject<List<String>> dataSet = BehaviorSubject.create(Collections.emptyList());

// In ListActivity
@Override protected void onCreate(Bundle savedInstanceState) {
    ArrayAdapter<String> adapter = new ArrayAdapter();
    setAdapter(adapter);
    dataSet.observeOn(AndroidSchedulers.mainThread())
            .subscribe(list -> {
                // This block called after onResume()
                adapter.clear();
                adapter.addAll(list);
            });
}

In this case, ListView can't restore scroll position when re-created.

ImmediateLooperScheduler avoids this issue because it executes actions immediately when actions emitted on same Looper.

License

Copyright 2016 Masatoshi Kubode

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.