Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ViewModel Scope #35

Closed
wants to merge 1 commit into from
Closed

ViewModel Scope #35

wants to merge 1 commit into from

Conversation

potibor
Copy link

@potibor potibor commented Jan 5, 2021

viewModelScope is added
bgScope and uiScope removed

viewModelScope is added
@potibor potibor requested a review from fatihergin January 5, 2021 07:31
@potibor potibor linked an issue Jan 5, 2021 that may be closed by this pull request
@potibor potibor changed the title Network scope change ViewModel Scope Jan 5, 2021
@titanwalking titanwalking self-requested a review April 20, 2021 14:13
Comment on lines +25 to +26
val backgroundJob = scope.async { run(params) }
scope.launch { onResult(backgroundJob.await()) }
Copy link

@titanwalking titanwalking Apr 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to use async{} here? Wouldn't

        scope.launch {
            val result = run(params)
            onResult(result)
        }

suffice?

Comment on lines +31 to +34
_loginInProgress.value = true

val username = username.value ?: return
val password = password.value ?: return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 31 should be below line 33 & 34 since one of them could return from the function and _loginInProgress.value would hang with true

nowPlayingMoviesResult.either(::handleFailure, ::postNowPlayingMovieList)
}
fetchNowPlayingMoviesUseCase.invoke(viewModelScope, UseCase.None) {
it.either(::handleFailure, ::postNowPlayingMovieList)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postNowPlayingMovieList() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postNowPlayingMovieList()

popularMoviesResult.either(::handleFailure, ::postPopularMovieList)
}
fetchPopularMoviesUseCase.invoke(viewModelScope, UseCase.None) {
it.either(::handleFailure, ::postPopularMovieList)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postPopularMovieList() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postPopularMovieList()

movieDetailResult.either(::handleFailure, ::postMovieDetail)
}
fetchMovieDetailUseCase.invoke(viewModelScope, FetchMovieDetailUseCase.Params(id)) {
it.either(::handleFailure, ::postMovieDetail)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postMovieDetail() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postMovieDetail()

movieCreditsResult.either(::handleFailure, ::postMovieCredits)
}
fetchMovieCreditsUseCase.invoke(viewModelScope, FetchMovieCreditsUseCase.Params(id)) {
it.either(::handleFailure, ::postMovieCredits)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postMovieCredits() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postMovieCredits()

viewModelScope,
FetchPersonDetailsUseCase.Params(personId)
) {
it.either(::handleFailure, ::postPersonDetails)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postPersonDetails() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postPersonDetails()

loginStateResult.either(::handleFailure, ::handleLoginStateSuccess)
}
getLoginStateUseCase.invoke(viewModelScope, UseCase.None) {
it.either(::handleFailure, ::handleLoginStateSuccess)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleLoginStateSuccess() calls postLoginState() inside. postLoginState() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postLoginState(). Also, handleLoginStateSuccess() calls updateUserDetails() which is a network operation and switches context with Dispatchers.IO. Using postValue() at postLoginState() would better suit here.

userDetailsResult.either(::handleFailure, ::postUserDetails)
}
fetchUserDetailsUseCase.invoke(viewModelScope, UseCase.None) {
it.either(::handleFailure, ::postUserDetails)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postUserDetails() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postUserDetails()

viewModelScope,
MultiSearchUseCase.Params(query)
) {
it.either(::handleSearchFailure, ::postMultiSearchResult)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postMultiSearchResult() uses LiveData's setValue() functions which must be called from main thread. Either we need to use postValue() on both LiveDatas we wan't their values to change, or switch context when calling postMultiSearchResult()

topRatedTvShowsResult.either(::handleFailure, ::postTopRatedTvShows)
}
fetchTopRatedTvShowsUseCase.invoke(viewModelScope, UseCase.None) {
it.either(::handleFailure, ::postTopRatedTvShows)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postTopRatedTvShows() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postTopRatedTvShows()

nowPlayingTvShowsResult.either(::handleFailure, ::postNowPlayingTvShows)
}
fetchNowPlayingTvShowsUseCase.invoke(viewModelScope, UseCase.None) {
it.either(::handleFailure, ::postNowPlayingTvShows)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postNowPlayingTvShows() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postNowPlayingTvShows()

tvShowDetailResult.either(::handleFailure, ::postTvShowDetail)
}
fetchTvShowDetailUseCase.invoke(viewModelScope, FetchTvShowDetailUseCase.Params(id)) {
it.either(::handleFailure, ::postTvShowDetail)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postTvShowDetail() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postTvShowDetail()

tvShowCreditResult.either(::handleFailure, ::postTvShowCredits)
}
fetchTvShowCreditsUseCase.invoke(viewModelScope, FetchTvShowCreditsUseCase.Params(id)) {
it.either(::handleFailure, ::postTvShowCredits)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postTvShowCredits() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postTvShowCredits()

Copy link

@titanwalking titanwalking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work. After this PR merges, I'm planning to migrate the project from LiveData to StateFlow. Then we will be able to set value inside the coroutine.

@potibor potibor closed this Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

viewModelScope instead of separate ui and bg scopes
2 participants