Demoz_Dev
Demoz_Dev

Reputation: 117

Convert Rx apply with Retrofit to lambda in Kotlin

I'm not familiar with using lambda expression in Kotlin. I successfully make network call using Rx apply with Retrofit in Kotlin, but IDE warm me that my code can convert to lambda. could you give me some idea with below code?

disposable = publishSubject
            .debounce(300, TimeUnit.MILLISECONDS)
            .switchMap(object: Function<String, Observable<ArrayList<Contact>>> {
                override fun apply(t: String): Observable<ArrayList<Contact>> {
                    return apiService.getContactList("", t)
                }
            })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(object: DisposableObserver<ArrayList<Contact>>() {
                override fun onNext(t: ArrayList<Contact>) {
                    adapter?.addItem(t)
                }

                override fun onError(e: Throwable) {
                    Toast.makeText(this@MainActivity, e.message, Toast.LENGTH_SHORT).show()
                }
                override fun onComplete() {

                }
            })

Upvotes: 1

Views: 98

Answers (1)

musooff
musooff

Reputation: 6852

If your IDE showing that it can be converted to lambda it will probably does it instead of you.

As an example for Android Studio. Here is how your code will look like and how you convert it to lambda.

enter image description here

So if you click on that line that has wavy line below it. You will get yellow lamb icon next to it.

enter image description here

If you click on Convert to lambda your IDE will convert your code to lambda equivalent.

enter image description here

Equivalently, you can do this with a shortcut Option + Enter on Mac.

Upvotes: 1

Related Questions