从合并的Observable.just和Subject中观察不到任何东西

时间:2018-03-15 14:52:13

标签: kotlin mvp rx-java2

我有一个从演示者到存储库的调用链,它返回一个observable。这是代码:

主讲人:

private fun getCategories() =
        compositeDisposable.add(
            categoriesUseCase.getCategories()
                .timeout(TIMEOUT, TIMEOUT_UNIT)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::handleCategories, this::handleCategoriesTimeout)
        )

这是用例:

fun getCategories(): Observable<List<Category>> =
        repository.getCategories()
            .map { it.map { Category(it.id, it.text, it.icon) } }

这是repo:// subject是BehaviorSubject.create()

    fun getcategories(): Observable<List<DiscoverabilityCategoryElement>> =
            Observable.just(storage.getCategories())
                .mergeWith { subject.flatMapIterable { it.categories }.publish() }
                .subscribeOn(Schedulers.io())
                .doOnNext { Logger.d("Data", "next categories $it") }
                .filter { it.isPresent }
                .map { it.get() }
                .take(1)
                .doOnSubscribe { Logger.d("Data", "Subcribed categories") }


   fun saveApiResult(response: Response) {//This is being called after subscribe
        subject.onNext(response.categories)
        subject.onComplete()
    }

存储方法将始终返回Optional.empty()(同时我正在开发)

我的问题是,即使看到subject.onNext正在被调用,该值永远不会出现在演示者身上,我调试了一下并且主题总是返回false给hasObservables,也许我在某些时候失去了我的观察者?

2 个答案:

答案 0 :(得分:0)

为什么要在该线路上拨打publish()?它返回ConnectableObservable,在调用connect之前不会执行任何操作。但是,该行上没有任何内容需要共享。

试试这个:

fun getcategories(): Observable<List<DiscoverabilityCategoryElement>> =
        Observable.just(storage.getCategories())
            .mergeWith { subject.flatMapIterable { it.categories } } // <-------------
            .subscribeOn(Schedulers.io())
            .doOnNext { Logger.d("Data", "next categories $it") }
            .filter { it.isPresent }
            .map { it.get() }
            .take(1)
            .doOnSubscribe { Logger.d("Data", "Subcribed categories") }

答案 1 :(得分:0)

解决方案是改变

<button (click)="change(0)">First</button>
<button (click)="change(1)">Second</button>
<button (click)="change(2)">Third</button>

<p:tabView [activeIndex]="index" ...


public index: number;

change(index: number): void {
    this.index = index;
}

通过

 .mergeWith { subject.flatMapIterable { it.categories }.publish() }