How can I customize my own Observable?

user3034559

I'm using retrofit2, rxjava2 and adapter-rxjava to implement my http api call.

//API definition
Observable<String> queryProducts(@Body Query query);

//API implementation.
serviceApi.queryProducts(query)
                .subscribeOn(new Scheduler().ioThread())
                .observeOn(new Scheduler().mainThread())
                .subscribe(new Observer());

If I have a lot of apis need to be implemented, and every individual api implementation needs to add these two lines:

.subscribeOn(new Scheduler().ioThread())
.observeOn(new Scheduler().mainThread())

I don't want to add them in every api implementation. I'd like to use MyObservable as to be the result type of my api definition.

My idea looks like below:

//API definition
 MyObservable<String> queryProducts(@Body Query query);

//MyObservable definition
    public class MyObservable<T> extends Observable<T> {
        /**
         * Creates an Observable with a Function to execute when it is subscribed to.
         * <p>
         * <em>Note:</em> Use {@link #create(OnSubscribe)} to create an Observable, instead of this constructor,
         * unless you specifically have a need for inheritance.
         *
         * @param f {@link OnSubscribe} to be executed when {@link #subscribe(Subscriber)} is called
         */
        protected MyObservable(OnSubscribe<T> f) {
            super(f);
            this.subscribeOn(new Scheduler().ioThread());
            this.observeOn(new Scheduler().mainThread());
        }
    }

When I run it, I got below exception:

java.lang.IllegalArgumentException: Unable to create call adapter for MyObservable.

I traced RxJavaCallAdapterFactory.java code at https://github.com/square/retrofit/blob/master/retrofit-adapters/rxjava/src/main/java/retrofit2/adapter/rxjava/RxJavaCallAdapterFactory.java. I found RxJavaCallAdapterFactory at line 100, it seems it only lets Observable class pass this checkpoint. I couldn't extend and override this method because this class is a final class.

if (rawType != Observable.class && !isSingle && !isCompletable) {
      return null;
}

Is there any way to add these two line in a super class, I don't want to add them in every api implementation? Thanks so much.

yosriz

While in RxJava2 you can safely extend Observable, it less likely suits for this kind of situation where you want to reuse common code instead of duplicating it (but for creating Observable from scratch, usually for wrapping external async callback code).
Instead you can use compose() operator which transform Observable with your custom code, and it's classic for adding common logic to Observable.
You can follow Dan Lew's article for a example exactly for your need (adding Schedulers).

Regrading retrofit adapter, as it's creating services with reflection it can't support custom classes but generates the existing RxJava classes.
BTW, you're looking/using retrofit's RxJava1 adapter with RxJava2, you need to switch to RxJava2 adapter, with RxJava2 adapter you can see that actually retrofit uses its own custom Observable classes.

If using compose() isn't enough for you (as you still need to add it to every API), the official way it to create your own CallAdapter.Factory and implement retrofit CallAdapter by wrapping RxJava2CallAdapterFactory delegating adapt to it, and then wrap the return Observable with your custom code/operators/schdeulers. see this tutorial. or example with RxJava2 (pretty the same) at some library I'm working on.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can I customize own brushstyle?

How can I customize my GTK theme?

How can I return my own promise?

How can I host my own website

How can I generate my own ScalaSig?

how can I spider my own website

How can i customize Output of My Database for my discord bot?

How can I Customize my email sender icon?

How can I customize my Service Worker based on environment variables?

How can I customize the selection state of my UICollectionViewCell subclass?

How can I Customize woocommerce "My Account" page?

How can I customize the creation of my Service Manifest file?

How can I customize my reducers using @ngrx/data?

GitHub: How can I transfer a repo that I own to an organization I own and keep it on my private account?

How can I sell to my own nft market with my own crypto token

How can I set up my own VPN proxy?

How can I create my own BaseEntity using PanacheEntityBase?

How can I use my own build of a package with stack?

How can I use realm with UITableViewController with my own sorting order?

How can I get Jackson to deserialize into my own Array implementation

How can I push my own numeric data to prometheus database?

UserDefaults How can I get my own saved keys?

How can I make my own base image for Docker?

How can I implement the miniplayer in Apple Music in my own app?

How can I use my own icons in dijit.menuItems?

Scala slick how can I return my own Error message

How can I run my own personalized bash shell in Java?

How can I edit Font Awesome (or include my own icons)?

How can I properly import Sass on my own CSS?