在MVP上使用Retrofit2 / RxJava2的正确方法

时间:2017-06-28 18:23:33

标签: android rx-java retrofit2 mvp rx-android

几天后,我可以独立使用RxJava2和Retrofit2。现在我正在尝试用RxJava2和Retrofit2来应对清洁架构的挑战。我在本地计算机上为一个简单的REST服务做了一个nodejs服务器。

这是我到目前为止所做的,它工作正常,但我正在寻找正确的方法来做到这一点。

在我的演示者课程中:

private void initLoadContentRx(){
    Observable.just("") //I havent been able to send void even if I dont need an input
            .subscribeOn(Schedulers.newThread())//moving to another thread
            .map(new Function<String, List<BImagenes>>() { //interface for input,output.
                @Override
                public List<BImagenes> apply(String cad){ // cad is the input value
                    Log.d(TAG,"long operation in thread: "+Thread.currentThread().getName()); //making sure we are in a second thread
                    return  loadContentWithRetrofit();
                }
            })
            .observeOn(AndroidSchedulers.mainThread()) // Get the results in Main Thread
            .subscribe(observer); //Observer which get notified when the results are done.
}

观察:

Observer observer = new Observer() {
    @Override
    public void onSubscribe(Disposable d) { Log.d(TAG,"onSubscribe, thread name >> "+Thread.currentThread().getName()); }

    @Override
    public void onNext(Object value) {
        Log.d(TAG,"on next,  thread name >> "+Thread.currentThread().getName());
        try{
            List<BImagenes> list = (List<BImagenes>) value;
            MySingleton.setList(list);//save the results in a Singleton
            Log.d(TAG,"i got my list parsed as POJO, with retrofit in thread >> "+Thread.currentThread().getName()+", list size >> "+list.size());
        }catch (Exception e){
            Log.d(TAG,"error in parsing >> "+e.toString());
        }

    }

    @Override
    public void onError(Throwable e) { Log.d(TAG,"onError error >>"+e.toString()); }

    @Override
    public void onComplete() {
        Log.d(TAG,"onCompleted  thread name >> "+Thread.currentThread().getName());
        presenterComm.showListView(MySingleton.getList());//Throught a communication interface implemented in Activity, I update the ListView
    }
};

因此,在我得到结果后,我通过此Activity实现的接口类实例更新了Activity中的ListView。

gradle dependecies

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

感谢您的时间

1 个答案:

答案 0 :(得分:0)

使用MVP模式使用RxJava2 / RxAndroid实现Retrofit2的正确方法,您需要使用observer subscriber方法清除它。要了解更好的MVP模式,请查看此示例示例:

 interface BaseContract {
    interface BaseView {
        //Methods for View
        void onDoSomething();
    }

    interface BasePresenter {
        void doSomething();

    }
}

class BaseMainPresenter implements BaseContract.BasePresenter {
    BaseContract.BaseView view;

    BaseMainPresenter(BaseContract.BaseView view) {
        this.view = view;
    }

    @Override
    public void doSomething() {
        if (view != null)
            view.onDoSomething();
    }
}

class DemoClass implements BaseContract.BaseView {

    //Create object of Presenter 

    /****
     * Example :
     * BaseMainPresenter baseMainPresenter = new BaseMainPresenter(this);
     */
    @Override
    public void onDoSomething() {
        //Deal with Context here.
    }
}

关于这个MVP模式你需要使用RxJava实现Retrofit2你可以参考我的一个参考文件的反思