Chain of responsibility in RxAndroid

2019-09-01 05:16发布

问题:

I want to make a three tasks running successively based on the parameter. Originally, I implemented this idea with asynctask but trouble comes a lot. I would like to change the idea to RxJava but I have no idea how to implement it.

Task A ( params: integer , response: String[] )

Task B ( params: String[] , response: long)

Task C ( params: long, response : boolean)

Task A -> B -> C

Can anyone give me a simple example how to implement it ? Thank you.

I found this library useful but I am not sure how to implement it.

回答1:

 public void main() {
        Observable.just(yourFirstMethod(42))
                .map(strings -> yourSecondMethod(strings))
                .map(aLong -> yourThirdMethod(aLong))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(aBoolean -> { 
                    ...
                }, throwable -> {
                    throwable.printStackTrace();
                });
    }

    public String[] yourFirstMethod(int param) {
        ...
    }

    public long yourSecondMethod(String[] param) {
        ...
    }

    public boolean yourThirdMethod(long param) {
        ...
    }