我是rxjava的新手,我对Flowable有疑问。
当onComplete
方法调用flatMap
时,未调用订阅者中的onNext
方法。这里有什么不对?有什么建议吗?
ResourceSubscriber<List<Song>> subscriber = new ResourceSubscriber<List<Song>>() {
@Override
public void onStart() {
Log.e(TAG, "onStart");
mView.showProgress();
}
@Override
public void onNext(List<Song> songs) {
mView.onLocalMusicLoaded(songs);
mView.emptyView(songs.isEmpty());
Log.e(TAG, "onNext");
//Not call here after onComplete called
}
@Override
public void onError(Throwable t) {
mView.hideProgress();
Log.e(TAG, "onError: ", t);
}
@Override
public void onComplete() {
mView.hideProgress();
Log.e(TAG, "onComplete");
}
};
Disposable disposable = Flowable.just(cursor)
.flatMap(new Function<Cursor, Publisher<List<Song>>>() {
@Override
public Publisher<List<Song>> apply(@NonNull Cursor cursor) throws Exception {
final List<Song> songs = new ArrayList<>();
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
do {
Song song = cursorToMusic(cursor);
songs.add(song);
} while (cursor.moveToNext());
}
return Flowable.create(new FlowableOnSubscribe<List<Song>>() {
@Override
public void subscribe(@NonNull FlowableEmitter<List<Song>> e) throws Exception {
e.onNext(songs);
e.onComplete();
}
}, BackpressureStrategy.LATEST);
}
})
.doOnNext(new Consumer<List<Song>>() {
@Override
public void accept(@NonNull List<Song> songs) throws Exception {
Log.e(TAG, "onLoadFinished doOnNext: " + songs.size());
Collections.sort(songs, new Comparator<Song>() {
@Override
public int compare(Song left, Song right) {
return left.getDisplayName().compareTo(right.getDisplayName());
}
});
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(subscriber);
mCompositeDisposable.add(disposable);
答案 0 :(得分:1)
来自ResourceSubscriber的Javadoc:
默认情况下,默认
onStart()
次请求Long.MAX_VALUE
。覆盖方法以请求自定义正数。使用受保护的request(long)
请求更多项目,dispose()
从onNext
实施中取消序列。
@Override
public void onStart() {
Log.e(TAG, "onStart");
mView.showProgress();
request(Long.MAX_VALUE);
}
您还成功找到Flowable.just
运算符,为什么通过Flowable.create
复制它以发出标量sogs列表?