英文:
Fetch data from Room with Flow
问题
Hi guys I cannot fetch data from my database:
I have in my DAO:
@Transaction
@Query("SELECT * FROM ClientEntity")
fun getClients(): Flow<List
RepoImpl:
override suspend fun getClients(): Flow<List
return clientDao.getClients()
}
ViewModel:
fun getClients(): Flow<List
val clients = mutableListOf
clientsRepository.getClients().collect { clientEntities ->
clients.addAll(clientEntities.map { it.toClient() })
}
emit(clients)
}
Screen:
LaunchedEffect(key1 = myContext) {
viewModel.getClients().collect{
val clients = viewModel.getClients()
Log.d(TAG, "ClientsListScreen: da $clients")
}
}
Im using jetpack compose, with this code I dont see logd in my Screen.
I know that are clients in my database since I inspected it.
英文:
Hi guys I cannot fetch data from my database:
I have in my DAO:
@Transaction
@Query("SELECT * FROM ClientEntity")
fun getClients(): Flow<List<ClientEntity>>
RepoImpl:
override suspend fun getClients(): Flow<List<ClientEntity>> {
return clientDao.getClients()
}
ViewModel:
fun getClients(): Flow<List<Client>> = flow {
val clients = mutableListOf<Client>()
clientsRepository.getClients().collect { clientEntities ->
clients.addAll(clientEntities.map { it.toClient() })
}
emit(clients)
}
Screen:
LaunchedEffect(key1 = myContext) {
viewModel.getClients().collect{
val clients = viewModel.getClients()
Log.d(TAG, "ClientsListScreen: da $clients")
}
}
Im using jetpack compose, with this code I dont see logd in my Screen.
I know that are clients in my database since I inspected it.
答案1
得分: 2
从所有地方删除getClients()
中的suspend
关键字
您还可以改进您的View Model
代码如下:
private val _clientResponse: MutableState<ClientState> = mutableStateOf(ClientState())
val clientResponse: State<ClientState> = _clientResponse
viewModelScope.launch {
repository.getClients()
.onStart {
_clientResponse.value = ClientState(
isLoading = true
)
}.catch {
_clientResponse.value = ClientState(
error = it.message ?: "Something went wrong"
)
}.collect {
_clientResponse.value = ClientState(
data = it
)
}
}
在Composable
函数中:
val clientResponse = viewModel.clientResponse.value
if (clientResponse.data.isNotEmpty())
// 做一些操作
if (clientResponse.error.isNotEmpty())
// 做一些操作
if (clientResponse.isLoading)
// 做一些操作
请注意,这些是您提供的代码的翻译部分。
英文:
First remove suspend keyword of getClients()
from everywhere
You can also improve your View Model
code like this
private val _clientResponse: MutableState<ClientState> = mutableStateOf(WeatherState())
val clientResponse: State<ClientState> = _clientResponse
viewModelScope.launch {
repository.getClients()
.onStart {
_clientResponse.value = ClientState(
isLoading = true
)
}.catch {
_clientResponse.value = ClientState(
error = it.message ?: "Something went wrong"
)
}.collect{
_clientResponse.value = ClientState(
data = it
)
}
}
data class ClientState(
val data: List< ClientEntity > = emptyList(),
val error: String = "",
val isLoading: Boolean = false
)
In Composable function
val clientResponse = viewModel.clientResponse.value
if (clientResponse.data.isNotEmpty())
// do something
if (clientResponse.error.isNotEmpty)
// do something
if(clientResponse.isLoading)
// do something
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论