Search edit text using rxJava. How to convert snippet of code?

I just copied snippet of code in java and automatically convert to kotlin using ide, but here is it my error: https://imgur.com/a/nHrVkDq

disposable.add(
                publishSubject
                        .debounce(300, TimeUnit.MILLISECONDS)
                        .distinctUntilChanged()
                        .switchMapSingle(new Function<String, Single<List<Contact>>>() {
                            @Override
                            public Single<List<Contact>> apply(String s) throws Exception {
                                return apiService.getContacts(null, s)
                                        .subscribeOn(Schedulers.io())
                                        .observeOn(AndroidSchedulers.mainThread());
                            }
                        })
                        .subscribeWith(observer));

Could you help me to convert it to kotlin?

Answer

It should go like this:

        disposable.add(
                publishSubject
                        .debounce(300, TimeUnit.MILLISECONDS)
                        .distinctUntilChanged()
                        .switchMapSingle { s ->
                            apiService.getContacts(null, s)
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                        }
                        .subscribeWith(observer))

Your Function must be coming from import io.reactivex.rxjava3.functions.Function

Leave a Reply

Your email address will not be published. Required fields are marked *