简体   繁体   English

意图有时不会被解雇

[英]Intent not getting fired sometimes

I have the following code: 我有以下代码:

public class CheckoutPresenter extends CheckinOutPresenter {

    @Inject
    public CheckoutPresenter(ThreadExecutor threadExecutor, PostExecutionThread postExecutionThread, OnlineBookingRepository onlineBookingRepository, OfflineBookingRepository offlineBookingRepository, ImageRepository imageRepository) {
        super(threadExecutor, postExecutionThread, onlineBookingRepository, offlineBookingRepository, imageRepository);
    }

    @Override
    protected void bindIntents() {
        Observable<CheckinOutViewState> checkinViewStateObservable =
                intent(CheckinOutView::fetchLocalCheckin)
                        .switchMap(uuid -> offlineBookingRepository.fetchCheckoutById(uuid, false)) <-- Here sometimes get called, sometimes not
                        .subscribeOn(Schedulers.from(threadExecutor))
                        .map(CheckinOutViewState.SuccessLocalFetch::new)
                        .cast(CheckinOutViewState.class)
                        .onErrorReturn(CheckinOutViewState.ErrorStateOut::new)
                        .observeOn(postExecutionThread.getScheduler());

        Observable<CheckinOutViewState> sendCheckin =
                intent(CheckinOutView::sendCheckin)
                        .switchMap(checkinIntent -> imageRepository.saveImage(checkinIntent.getFilename(), checkinIntent.getSignature())
                                .subscribeOn(Schedulers.from(threadExecutor))
                                .filter(cnhImageResponse -> cnhImageResponse.getLinks() != null)
                                .switchMap(response ->  {
                                    CheckinOutCommand command = checkinIntent.getCheckinOutCommand();
                                    command.setSignatureUrlImage(response.getLinks().getUrl());
                                    return onlineBookingRepository.doCheckout(command).subscribeOn(Schedulers.from(threadExecutor));
                                })
                                .subscribeOn(Schedulers.from(threadExecutor))
                                .map(CheckinOutViewState.SentSuccessfuly::new)
                                .cast(CheckinOutViewState.class)
                                .startWith(new CheckinOutViewState.LoadingStateOut())
                                .doOnError(error -> offlineBookingRepository.doCheckout(checkinIntent.getCheckinOutCommand()))
                                .doOnComplete(() -> offlineBookingRepository.deleteCheckoutById(checkinIntent.getCheckinOutCommand().getBookingId(), false))
                                .onErrorReturn(CheckinOutViewState.ErrorStateOut::new))
                        .observeOn(postExecutionThread.getScheduler());

        subscribeViewState(Observable.merge(checkinViewStateObservable, sendCheckin), CheckinOutView::render);
    }

}

which inherits from: 继承自:

public abstract class CheckinOutPresenter extends MviBasePresenter<CheckinOutView, CheckinOutViewState> {

    protected final ThreadExecutor threadExecutor;
    protected final PostExecutionThread postExecutionThread;
    protected final OnlineBookingRepository onlineBookingRepository;
    protected final OfflineBookingRepository offlineBookingRepository;
    protected final ImageRepository imageRepository;

    CheckinOutPresenter(ThreadExecutor threadExecutor, PostExecutionThread postExecutionThread, OnlineBookingRepository onlineBookingRepository, OfflineBookingRepository offlineBookingRepository, ImageRepository imageRepository) {
        this.threadExecutor = threadExecutor;
        this.postExecutionThread = postExecutionThread;
        this.onlineBookingRepository = onlineBookingRepository;
        this.offlineBookingRepository = offlineBookingRepository;
        this.imageRepository = imageRepository;
    }

}

@Override
    public Observable<String> fetchLocalCheckin() {
        return Observable.just(booking.getBookingId());
    }

But the problem is, sometimes the fetchLocalCheckin is fired, sometimes not. 但是问题是,有时会触发fetchLocalCheckin,有时却不会。 What am I doing wrong here ? 我在这里做错了什么? Did I miss something ? 我错过了什么 ?

@Override
public Observable<String> fetchLocalCheckin() {
     return Observable.just(booking.getBookingId());
}

Observable.just(...) . Observable.just(...) This one triggers exactly one time onNext() followed immediately by a onComplete() . 这一次恰好触发一次onNext()然后立即触发onComplete() Since Mosby's MviBasePresenter follows the Reactive contract which basically says that once a onComplete() event is received the whole stream "finishes", that means the CheckinOutView::fetchLocalCheckin only triggers exactly one time and cannot be invoked a second time (because onComplete() already has been triggered). 由于Mosby的MviBasePresenter遵循Reactive契约,该契约基本上说一旦接收到onComplete()事件,整个流就会“完成”,这意味着CheckinOutView::fetchLocalCheckin仅触发一次,并且无法再次调用(因为onComplete()已经被触发)。

So if you want to trigger multiple times this intent, don't use Observable.just() but rather PublishSubject or PublishRelay . 因此,如果您想多次触发此意图,请不要使用Observable.just()而是使用PublishSubjectPublishRelay

ie with your case, this one will only ever trigger the first time your View layer (Fragment, Activity, ViewGroup) has been created, but no second time, ie after screen orientation changes, because the first time it has been created Observale.just() triggers onComplete() which prevents MviBasePresenter from accepting future events. 即,对于您的情况,此视图仅会在第一次创建您的View图层(片段,活动,ViewGroup)时触发,而不会在第二次(即屏幕方向更改后)触发,因为它是第一次创建Observale.just()触发onComplete() ,以防止MviBasePresenter接受未来的事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM