Mosby,Conductor和Rx表现不佳

时间:2016-05-17 16:26:08

标签: android mvp mosby

我刚刚开始了一个新的示例项目来测试使用Mosby的指挥。 问题是,在我离开FirstScreen或旋转设备后,在Rx onNext回调之后,isViewAttached()总是返回false,我无法理解原因。 一些片段澄清:

public abstract class MvpLceRxPresenter<V extends MvpLceView<M>, M> extends MvpBasePresenter<V>
        implements MvpPresenter<V> {

    protected Subscriber<M> subscriber;

    protected void unsubscribe() {
        if (subscriber != null && !subscriber.isUnsubscribed()) {
            subscriber.unsubscribe();
        }

        subscriber = null;
    }

    public void subscribe(@NonNull UseCase useCase, @Nullable Bundle bundle, final boolean pullToRefresh) {
        if (isViewAttached()) {
            getView().showLoading(pullToRefresh);
        }

        useCase.unsubscribe();

        subscriber = new Subscriber<M>() {
            private boolean ptr = pullToRefresh;

            @Override
            public void onCompleted() {
                MvpLceRxPresenter.this.onCompleted();
            }

            @Override
            public void onError(Throwable e) {
                MvpLceRxPresenter.this.onError(e, ptr);
            }

            @Override
            public void onNext(M m) {
                MvpLceRxPresenter.this.onNext(m);
            }
        };

        if (useCase instanceof DynamicUseCase) {
            DynamicUseCase dynamicUseCase = (DynamicUseCase) useCase;
            dynamicUseCase.execute(subscriber, bundle);
        } else {
            useCase.execute(subscriber);
        }
    }

    public void subscribe(Observable<M> observable, final boolean pullToRefresh) {
        if (isViewAttached()) {
            getView().showLoading(pullToRefresh);
        }

        unsubscribe();

        subscriber = new Subscriber<M>() {
            private boolean ptr = pullToRefresh;

            @Override
            public void onCompleted() {
                MvpLceRxPresenter.this.onCompleted();
            }

            @Override
            public void onError(Throwable e) {
                MvpLceRxPresenter.this.onError(e, ptr);
            }

            @Override
            public void onNext(M m) {
                MvpLceRxPresenter.this.onNext(m);
            }
        };

        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);

    }

    protected void onCompleted() {
        if (isViewAttached())
            getView().showContent();
        unsubscribe();
    }

    protected void onError(Throwable e, boolean pullToRefresh) {
        if (isViewAttached())
            getView().showError(e, pullToRefresh);
        unsubscribe();
    }

    protected void onNext(M data) {
        if (isViewAttached())
            getView().setData(data);
    }

    @Override
    public void detachView(boolean retainInstance) {
        super.detachView(retainInstance);
        if (!retainInstance)
            unsubscribe();
    }

我的主持人:

@DaggerScope(FirstScreenComponent.class)
public class FirstScreenPresenter extends MvpLceRxPresenter<FirstView, List<Contact>> {

    private final GetContacts getContacts;

    @Inject
    public FirstScreenPresenter(GetContacts getContacts) {
        this.getContacts = getContacts;
    }

    public void fetchContacts(boolean mustHaveNumber) {
        Bundle bundle = new Bundle(1);
        bundle.putBoolean("mustHaveNumber", mustHaveNumber);

        subscribe(this.getContacts, bundle, false);
    }
}

FirstScreen的某些部分:

@Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        swapNumberOnlyText(menu.findItem(R.id.number_only));
    }

    private void swapNumberOnlyText(MenuItem item) {
        if(mustHaveNumber)
            item.setTitle("Todos contatos");
        else
            item.setTitle("Somente com número");
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId() == R.id.number_only){
            mustHaveNumber = !mustHaveNumber;
            this.presenter.fetchContacts(mustHaveNumber);
            swapNumberOnlyText(item);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
@Override
    public List<Contact> getData() {
        return adapter.getContacts();
    }

    @Override
    public void setData(List<Contact> data) {
        adapter.setContacts(data);
    }

    @Override
    protected String getErrorMessage(Throwable e, boolean pullToRefresh) {
        return null;
    }

    @NonNull
    @Override
    public LceViewState<List<Contact>, FirstView> createViewState() {
        return new RetainingLceViewState<>();
    }

    @NonNull
    @Override
    public FirstScreenPresenter createPresenter() {
        return presenter;
    }

    @Override
    public void loadData(boolean pullToRefresh) {
        this.presenter.fetchContacts(mustHaveNumber);
    }

让我烦恼的是,当我第一次打开时,我可以点击菜单项,它就像一个魅力,但如果我离开或旋转,描述的问题就会开始。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:3)

这不是Mosby-Conductor问题。这个问题是由匕首和你注入主持人的方式引起的。

您正在onViewBound()方法中的每个屏幕方向更改上创建并注入新的Presenter实例。这意味着每个屏幕方向都会创建一个新的演示者。

此外,由于您要为演示者声明自己的字段,因此您不使用Presenter的MvpLceViewStateController字段。请参阅:enter image description here

Mosby内部工作如下:

if (getPresenter() == null) {
     setPresenter(  createPresenter() ); 
}

在MvpController中(从getPresenter()扩展的MvpViewStateLceController的基类和setPresenter()正在使用MvpController.presenter字段。 https://github.com/sockeqwe/mosby-conductor/blob/master/mvp/src/main/java/com/hannesdorfmann/mosby/mvp/conductor/MvpController.java#L19

有两种解决方案:

  1. 不要声明你自己的presenter字段,而是做这样的事情(然后它将使用MvpController.presenter字段):

    @覆盖     public FirstScreenPresenter createPresenter(){         返回DaggerFirstScreenComponent.builder()             .activityComponent(((RootActivity)getActivity())。getActivityComponent())             .firstScreenModule(new FirstScreenModule())             。.build()主持人();     }

  2. 这确保只创建一次presenter实例,因为createPresenter()只会被调用一次。

    1. 如果您想使用自己的字段展示器,那么您还必须在控制器中覆盖从MvpLce扩展的setPresenter()和getPresenter()

      public class FirstScreen扩展了BaseController,FirstView,FirstScreenPresenter&gt;实现FirstView {

      private static final String MUST_HAVE_NUMBER = "must_have_number";
      @Inject
      protected FirstScreenPresenter presenter;
      

      @覆盖     public FirstScreenPresenter getPresenter(){         返回主持人;     }

      @Override
      public void setPresenter(FirstScreenPresenter p){
         this.presenter = p;
      }
      
    2. 由于Mosby内部工作的方式,它调用getPresenter(),createPresenter()和setPresenter()。但是,您必须确保不在onViewBound()方法中创建和注入新的Presenter。

      顺便说一下。如果您使用Mosby而不是Conductor

      的Activity / Fragments,也可能会遇到此问题