简体   繁体   中英

Conversion from Observable to Mono in Kotlin

Trying to insert in couchbase and that has observable return type but want mono,hence did this. Its compiling but at run time its getting stuck forever at the conversion stage (ie Mono.from { obs }).

    fun saveScopeId(scopeId: ScopeId): Mono<ScopeId> {        
    val obs = scopeRepository.couchbaseOperations.insert(scopeId)
    return Mono.from<ScopeId> { obs }
}

Observable can generate multiple values but if you can guarantee that it will be one item (I assume this is why you want to use Mono here) you can use Mono.fromDirect in this way:

Mono.fromDirect(yourObservable.toFlowable(BackpressureStrategy.BUFFER));

As you can see in example, there is toFlowable method used.

You should see the other backpressure strategies: here

This way we can achieve but not sure about the performance part.

As there Rx -> Rx -> Reactor conversion. Can someone tell me by looking into couchbase SDK 4.x (introduced recently), only if there are some conversion issue.

Mono.fromDirect(RxReactiveStreams.toPublisher(scopeRepository.couchbaseOperations.insert(scope)))

Try this but this thread blocking model.

public Mono<String> GetData(Observable<String> inputData) {
    return Mono.fromCallable(() -> inputData.blockingFirst(""));        
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM