我在我的应用程序中执行以下操作(请参阅注释以查看发生的情况,逻辑非常简单):
// 1) import folders from system
importObservable
// 2) emit all folders as list
// => 1 item is emitted only
// => flatMap can be used for sequential execution
.toList()
// 3) Log current time
.doOnNext(data -> T.start(KEY_TIME_LOGGER))
// 4) throw away data from import
// load all data from database now in an empty state
// => 1 item is emitted only
.flatMap(data -> loadFoldersEmpty())
// 5) Log time
.doOnNext(data -> L.d("Folder - 1 - empty list created: " + T.printTotalAndLap(KEY_TIME_LOGGER)))
// 6) observe the list of folders on the main thread
// because we will update the UI
// => 1 emitted item only
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(items -> onNextItemAvailable(items))
.observeOn(Schedulers.io())
// 7) Log time
.doOnNext(data -> L.d("Folder - 2 - empty data delivered: " + T.printTotalAndLap(KEY_TIME_LOGGER)))
// 8) loading full folders
// => this emits multiple items, folder per folder
.flatMap(data -> loadFullFolders(data))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Object>() {
@Override
public void onCompleted() {
onLoadingFinished();
}
@Override
public void onError(Throwable e) {
onLoadingError(e);
}
@Override
public void onNext(Object o) {
onNextItemAvailable(o);
}
});
问题是,评论#6后发生的事情很慢(约200ms到300ms)。是否有更好的方法在主线程上产生副作用?