从 Room 中使用 Flow 获取数据

huangapple go评论55阅读模式
英文:

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> = flow {
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(&quot;SELECT * FROM ClientEntity&quot;)
    fun getClients(): Flow&lt;List&lt;ClientEntity&gt;&gt;

RepoImpl:

 override suspend fun getClients(): Flow&lt;List&lt;ClientEntity&gt;&gt; {
        return clientDao.getClients()
    }

ViewModel:

fun getClients(): Flow&lt;List&lt;Client&gt;&gt; = flow {
        val clients = mutableListOf&lt;Client&gt;()
        clientsRepository.getClients().collect { clientEntities -&gt;
            clients.addAll(clientEntities.map { it.toClient() })
        }
        emit(clients)
    }

Screen:

LaunchedEffect(key1 = myContext) {  
            viewModel.getClients().collect{
                val clients = viewModel.getClients()
                Log.d(TAG, &quot;ClientsListScreen: da $clients&quot;)
            }
        
    }

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&lt;ClientState&gt; = mutableStateOf(WeatherState())
    val clientResponse: State&lt;ClientState&gt; = _clientResponse


viewModelScope.launch {
                repository.getClients()
                    .onStart {
                         _clientResponse.value = ClientState(
                    isLoading =  true
                )
                    }.catch {
                         _clientResponse.value = ClientState(
                    error = it.message ?: &quot;Something went wrong&quot;
                )
                    }.collect{
                         _clientResponse.value = ClientState(
                    data = it
                )
                    }
            }


data class ClientState(
    val data: List&lt; ClientEntity &gt; = emptyList(),
    val error: String = &quot;&quot;,
    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

huangapple
  • 本文由 发表于 2023年2月27日 01:17:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定