question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[MVI] intent(MviView::intent) does't fire before view is attached

See original GitHub issue

My question is somehow similar to this #242 So I have an intent in my view implemented like this:

override fun selectDateIntent(): Observable<Day> {
       return selectDatePubSub.distinctUntilChanged()
               .startWith(selectedDate)
               .doOnNext {
                       //this fires one time correctly because off startWith()
               }
   }

In the presenter I have this observable wrapped in the intent() function like this:

timetableInteractor.setSelectDateIntent(intent(TimetableView::selectDateIntent)} )

After debugging I noticed that the observable resulting from the wrapped intent does not emit nothing before the view is attached. So, if I add a delay to the intent implementation in the view, everything goes as expected. However, in the MVI sample code having Observable.just(object) is a recurring practice for the intent implementations, so I am curious to know what I’m doing incorrectly.

Thanks, Diogo

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
kusraevscommented, Feb 5, 2018

Binding intents actually starts in attachView method of MviBasePresenter. In MviActivity this method calls in onStart. So if you use PublishSubject item could be emitted before this time because it is a hot observable. If you use Observable.just(…) we don’t have this problem. In this situation i use BehaviorSubject instead of PublishSubject.

But if you call startWith inside intent() function it should work ok. So this is issue is the same as #242 as far as i concern

1reaction
sockeqwecommented, Feb 23, 2018

I will work on that next week

jhowens89 notifications@github.com schrieb am Fr., 23. Feb. 2018, 01:51:

@sockeqwe https://github.com/sockeqwe Did you ever come to a conclusion about #242 https://github.com/sockeqwe/mosby/issues/242? I was still very new to RxJava back then and I’d like to think the code I write down is less problematic, but I can still replicate this issue.

class MatchPlayRoundSelectorPresenter @Inject constructor(val interactor: MatchPlayRoundSelectorInteractor) : MviBasePresenter<MatchPlayRoundSelectorView, MatchPlayRoundSelectorViewState>() { override fun bindIntents() { val loadMatchPlayRoundSelectorDataIntent = intent(MatchPlayRoundSelectorView::connectDataIntent) .doOnNext { Timber.e(“TESTING_BEFORE 1”) } .switchMap { interactor.loadMatchPlayRoundSelectorData() } .doOnNext { Timber.e(“TESTING_BEFORE 2”) } .subscribeOn(Schedulers.io()) .doOnError { Timber.e(it,“Fatal error occurred”) }

    val selectNewRoundIntent = intent(MatchPlayRoundSelectorView::selectNewRoundIntent)
            .map { (round, page) -> interactor.newRoundSelected(round, page) }

    val initialState = interactor.getInitialState()

    val allIntents = Observable.merge(loadMatchPlayRoundSelectorDataIntent, selectNewRoundIntent)
            .observeOn(AndroidSchedulers.mainThread())

    subscribeViewState(allIntents.scan(initialState, ::viewStateReducer)
            .distinctUntilChanged(), MatchPlayRoundSelectorView::render)}

}

The other side of the equation looks like:

override fun connectDataIntent(): Observable<Unit> = Observable.just(Unit)//.doOnNext { Timber.e(“TESTING_BEFORE 0”) }

If I run this code, I’ll get the following relevant logs: 02-22 17:46:12.605 7068-7068/com.tour.pgatour E/MatchPlayRoundSelectorL: TESTING_BEFORE render com.tour.pgatour.match_play.leaderboard.match_play_round_selector.MatchPlayRoundSelectorViewState$WithoutData@7830fd3

If all I do is uncomment that doOnNext:

02-22 17:44:57.205 6216-6216/com.tour.pgatour E/MatchPlayRoundSelectorL: TESTING_BEFORE render com.tour.pgatour.match_play.leaderboard.match_play_round_selector.MatchPlayRoundSelectorViewState$WithoutData@fb867e3 02-22 17:44:57.206 6216-6216/com.tour.pgatour E/MatchPlayRoundSelectorL: TESTING_BEFORE 0 02-22 17:44:57.206 6216-6216/com.tour.pgatour E/MatchPlayRoundSelectorP: TESTING_BEFORE 1 02-22 17:44:57.368 6216-6216/com.tour.pgatour E/MatchPlayRoundSelectorP: TESTING_BEFORE 2 02-22 17:44:58.099 6216-6216/com.tour.pgatour E/MatchPlayRoundSelectorL: TESTING_BEFORE render WithData(viewData=MatchPlayRoundSelectorViewData(roundPlayTabs=[RoundTab(config=TabConfig(text=Round 1, backgroundColor=-16777216, textColor=-1, selectedBackgroundColor=-1, selectedTextColor=-16777216), option=com.tour.pgatour.shared_rel…

It’s like I’m so close to the boundary of the race condition that even a log will push it over. Thoughts?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/sockeqwe/mosby/issues/303#issuecomment-367873536, or mute the thread https://github.com/notifications/unsubscribe-auth/AAjnrsjsgKn0wFcqCDFcchSygyQi2p13ks5tXgt5gaJpZM4Rz2xJ .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android MVI (Model-View-Intent) Architecture — Example code
MVI stands for Model-View-Intent but this word “intent” doesn't refers to the Android Intents that we know. Let's breakdown the main 3 layers....
Read more >
Reactive Apps with Model-View-Intent - Part 2
Model-View-Intent MVI on Android by using Mosby 3. ... Before we start to connect the dots lets quickly discuss the main idea of...
Read more >
LiveData vs SharedFlow and StateFlow in MVVM and MVI ...
MVI stands for Model–View–Intent and it's a design pattern that ... We have SharedFlow, StateFlow, but we had Flow already in Kotlin before....
Read more >
Why MVI? Model View Intent -- The curious case of ... - YouTube
In this talk:- We will learn about the main problem that this pattern solves - The State Problem, What it is and how...
Read more >
Building An Android App With MVI - YouTube
Broadcasted live on Twitch -- Watch live at https://www.twitch.tv/adammc331You can find the source code here: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found