Jude Fernandes
Jude Fernandes

Reputation: 7517

Retrofit call within Stream

I have an edit text which allows a user to input a username and once the username is input the value is sent to the db to check whether the username already exists,if not then further operations are allowed else an error is displayed.

As of now this is my current code.

usernameObservable
            .skip(2)
            .debounce(800, TimeUnit.MILLISECONDS)
            .subscribe(username -> {
                Observable<Boolean> observable = apiService.isAvailable(username);
                observable.observeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(aBoolean -> {

                        }, throwable -> {

                        });
            });

For now the network request is being made at the end but is it possible to make the request before and once i receive data i perform some other operations on the stream.

Upvotes: 0

Views: 519

Answers (1)

koperko
koperko

Reputation: 2487

You are looking for flatMap operator. It allows you to transform an event into another observable, which will forward emissions to the original stream. You error notification will be forwarded as well.

usernameObservable
        .skip(2)
        .debounce(800, TimeUnit.MILLISECONDS)
        .flatMap(username -> apiService.isAvailable(username))
        .subscribe(isAvailableResult -> {
            // react here
        }, throwable -> {
            // show an error here
        });

Upvotes: 2

Related Questions