最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Reading two values from datastore flow correctly - Stack Overflow

programmeradmin2浏览0评论

I have a screen where I wan't to show a loading circle until I readed the default text values for two variables stored in datastore. I'm getting these values in a coroutine in the init block of the viewmodel of the screen. And I update loading to false after the collect of these two flows.

The problem is that for reading these values from the flows returned by datastore, I'm using collect, and collect doesn't end, so the loading circle is never disabled. The idea is to put to false the loading if no variables are on the flows or if one variable is returned by each flow. How to solve this situation?

init {
    viewModelScope.launch(Dispatchers.IO) {
        dataStoreRepository.readString("KEY").collect { value ->
            updateKey(value)
        }

        dataStoreRepository.readString("NAME").collect { value ->
            updateName(value)
        }

        _uiState.update { currentState ->
            currentState.copy(loading = false)
        }
    }
}

I have a screen where I wan't to show a loading circle until I readed the default text values for two variables stored in datastore. I'm getting these values in a coroutine in the init block of the viewmodel of the screen. And I update loading to false after the collect of these two flows.

The problem is that for reading these values from the flows returned by datastore, I'm using collect, and collect doesn't end, so the loading circle is never disabled. The idea is to put to false the loading if no variables are on the flows or if one variable is returned by each flow. How to solve this situation?

init {
    viewModelScope.launch(Dispatchers.IO) {
        dataStoreRepository.readString("KEY").collect { value ->
            updateKey(value)
        }

        dataStoreRepository.readString("NAME").collect { value ->
            updateName(value)
        }

        _uiState.update { currentState ->
            currentState.copy(loading = false)
        }
    }
}
Share Improve this question asked Mar 12 at 12:05 NullPointerExceptionNullPointerException 37.8k80 gold badges231 silver badges405 bronze badges 3
  • Can you not just combine the 2 flows and only emit to _uiState when they both produce values? – Ivan Wooll Commented Mar 12 at 14:12
  • @IvanWooll can you post an answer with your proposal? what will happen if there is no saved value with that proposal? and what if one is returned but other not? – NullPointerException Commented Mar 12 at 14:39
  • Please edit the question to also provide what updateKey and updateName do. You generally don't want to collect flows in the view model, so whatever you do here should be done some other way. Please also provide _uiState and how it is updated elsewhere. – tyg Commented Mar 12 at 17:25
Add a comment  | 

1 Answer 1

Reset to default 0

I would use the combine extension function for this. The below example should hopefully demonstrate how it could be done. Replace keyStringFlow & nameStringFlow with your datastore flows

val keyStringFlow = MutableStateFlow("")
val nameStringFlow = MutableStateFlow("")
val booleanFlow: Flow<Boolean> = keyStringFlowbine(nameStringFlow) { key, name ->
    return@combine key.isNotEmpty() && name.isNotEmpty()
}

init {
    viewModelScope.launch {
        booleanFlow.collect { boolean ->

        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论