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
.
-
Override
AndroidSchedulers.mainThread()
withRxAndroidPlugins
.public class MyApplication extends Application { @Override protected void onCreate() { RxAndroidPlugins.setInitMainThreadSchedulerHandler(schedulerCallable -> ImmediateLooperScheduler.MAIN); } }
-
Replace
AndroidSchedulers.mainThread()
toImmediateLooperScheduler.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.