Reputation: 117
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
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.
So if you click on that line that has wavy line below it. You will get yellow lamb icon next to it.
If you click on Convert to lambda
your IDE will convert your code to lambda equivalent.
Equivalently, you can do this with a shortcut Option + Enter
on Mac.
Upvotes: 1