Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Comments

Close side panel

Сделал ДЗ № 15#8

Open
anv0l wants to merge 5 commits intoAndroid-Developer-Basic:masterAndroid-Developer-Basic/CoroutinesHomework:masterfrom
anv0l:masteranv0l/CoroutinesHomework:masterCopy head branch name to clipboard
Open

Сделал ДЗ № 15#8
anv0l wants to merge 5 commits intoAndroid-Developer-Basic:masterAndroid-Developer-Basic/CoroutinesHomework:masterfrom
anv0l:masteranv0l/CoroutinesHomework:masterCopy head branch name to clipboard

Conversation

@anv0l
Copy link

@anv0l anv0l commented May 22, 2025

No description provided.

viewModelScope.launch(Dispatchers.IO) {
LoginApi().logout()
}
_state.value = LoginViewState.Login()

Choose a reason for hiding this comment

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

Обратите внимание, что у вас состояние экрана меняется синхронно, а логаут выполняется асинхронно. Соответственно, состояние экрана не соответствует реальному состоянию системы. Логаут может занять несколько секунд и вернуть ошибку, например

fun login(name: String, password: String) {
// TODO: Implement login
_state.value = LoginViewState.LoggingIn
viewModelScope.launch(Dispatchers.IO) {

Choose a reason for hiding this comment

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

Мне кажется, удобнее было бы сделать так:

viewModelScope.launch {
   runCatching {
      // Переходим на другой контекст
      withContext(Dispatchers.IO) { LoginApi().login(...) }.fold (
          { 
            // Здесь мы снова в изначальном контексте
             _state.value =  _state.value = LoginViewState.Content(it) 
          },
          { _state.value =  LoginViewState.Login(error = it) }
       )
   }
}

Помните, что контекст корутины восстанавливается на основной после вызова withContext. И запускать корутину для обновления состояния экрана - не нужно

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.

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.