Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert this rxjava/rxkotlin flatMap into lambda expression?

    Observable.just(1)
            .flatMap(object : Function<Int, Observable<Int>> {
                override fun apply(integer: Int): Observable<Int> {
                    return Observable.just(integer * 10)
                }
            })
            .flatMap(object : Function<Int, Observable<Int>> {
                override fun apply(integer: Int): Observable<Int> {
                    return Observable.just(integer * 20)
                }
            })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<Int> {
                override fun onComplete() {
                }

                override fun onSubscribe(d: Disposable) {
                }

                override fun onNext(t: Int) {
                    Log.d("result", "" + t)
                }

                override fun onError(e: Throwable) {
                    e.printStackTrace()
                }
            })
like image 540
s-hunter Avatar asked Feb 12 '18 18:02

s-hunter


People also ask

What does FlatMap do in RxJava?

FlatMap transforms the items emitted by an Observable into Observables. So, the main difference between Map and FlatMap is that FlatMap mapper returns an observable itself, so it is used to map over asynchronous operations. Very important: FlatMap is used to map over asynchronous operations.

What is the difference between coroutines and RxJava?

Coming to RxJava, RxJava streams are prone to leaks, where a stream continues to process items even when you no longer care. Kotlin coroutines use structured concurrency, which makes it much easier to manage the lifecycle of all your concurrent code.

Can I use RxJava with Kotlin?

RxJava can be used even when using the Kotlin language for app development.

What is a disposable Kotlin?

A Disposable is a stream or a link between an Observable and an Observer . A quick check of the documentation shows that it has two main methods, dispose() and isDisposed() .


2 Answers

This should do.

Observable.just(1)
        .flatMap { 
            return@flatMap Observable.just(it*10)
        }.flatMap { 
            return@flatMap Observable.just(it*20)
        }.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
        .subscribe({
            //OnNext
           Log.d("result", "" + it)
        },{
          it.printStackTrace()
            //on error
        },{
            //on complete
        })
like image 68
karandeep singh Avatar answered Sep 29 '22 19:09

karandeep singh


actually, return@flatMap is not needed, so below works as well. Also, if you don't need all methods for a subscriber actually implemented, there's an overload with just onNext and onError. IDE's hints are of great help in here - when typing in a method, press Ctrl+P and it will show you available overloads. The keyboard shortcut is essentially "show me arguments".

    Observable.just(1)
        .flatMap { Observable.just(it * 10) }
        .flatMap { Observable.just(it * 20) }
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
                { Log.d("result", "" + it) },
                { it.printStackTrace() }
                  )
like image 41
Antek Avatar answered Sep 29 '22 17:09

Antek