Convert an Observable to Single rx java

时间:2019-01-09 22:00:03

标签: java observable rx-java

I am working with the next code:

override fun getSportList(dCSServiceContext: DCSServiceContext): Single<SportsResponse> {
        return scribeService.getNavMenuItems(dCSServiceContext).map { navMenu ->
            SportsResponse(navMenu as NavMenu)
        }
    }

And the getNavMenuItems looks like this:

override fun getNavMenuItems(dCSServiceContext: DCSServiceContext): Observable<NavItem> {
        return getItems(getNavItemById(dCSServiceContext), dCSServiceContext)
    }

The thing is that, I have to transform this Observable to a Single, I had used the toSingle() method, just like this:

override fun getSportList(dCSServiceContext: DCSServiceContext): Single<SportsResponse> {
        return scribeService.getNavMenuItems(dCSServiceContext).toSingle().map { navMenu ->
            SportsResponse(navMenu as NavMenu)
        }
    }

But I am getting the next error:

Observable emitted too many elements,because the Observable emits more than one element. Similarly expect "Observable emitted no items" if Observable is empty.

Any ideas?

0 个答案:

没有答案