1

I use RxBinding and I have a case where I make use of RxTextView's textChangeEvents() I filter them and pass them to switchMap() where I execute a network request (using Retrofit) to an endpoint based on another value.

tl;dr :

switchMap(new Func1<TextViewTextChangeEvent, Observable<List<String>>>() {
                @Override
                public Observable<List<String>> call(TextViewTextChangeEvent textViewTextChangeEvent) {

                    switch (visibleTab) {
                        case TAGS:
                            return presenter.executeSearchPostsByTag(searchBarText, String.valueOf(0));
                        case PEOPLE:
                            return presenter.executeSearchPostsByPeople(searchBarText, String.valueOf(0));
                        case COMPANIES:
                            return presenter.executeSearchPostsByCompanies(searchBarText, String.valueOf(0));
                        case JOBS:
                            return presenter.executeSearchPostsByJobs(searchBarText, String.valueOf(0));
                        default:
                            return presenter.executeSearchPostsByTag(searchBarText, String.valueOf(0));
                    }

                }
            })

but I can't have as a return type : Observable<List<String>> since I receive a different object from each request.

How should I deal with such a case ?

Mes
  • 1,671
  • 3
  • 20
  • 36

1 Answers1

3

One method is to use an intermediary Completable transformation to throw away all elements and just wait for the onComplete call.

For each item, add .toCompletable().<Void>toObservable() to turn the output into Observable<Void> instead of Observable<List<String>>. This works since completable ensures no elements are emitted, ensuring type-safety.

Another option that might work is declaring the output as Observable<?>, but those wildcards can cause some weird issues with generics.

Note that if you're using RxJava 2, then the call to ignore elements and just return nothing is .ignoreElements().<Void>toObservable()

Kiskae
  • 24,655
  • 2
  • 77
  • 74
  • Cool, I wasn't aware of : `.toCompletable().toObservable()`. Let me try it and I'll let you know. – Mes Dec 24 '16 at 22:48
  • The problem with this approach is that no items are emitted so I can't consume the responses to the subscriber. – Mes Dec 25 '16 at 14:24
  • You will need to define some common way to consume the output then. A single subscriber can't handle multiple disjoint data types – Kiskae Dec 25 '16 at 18:50