更新(或创建)自身内部的流并发出 this, Room, Flow, MVVM
Update (or create) flow inside itself and emit this, Room, Flow, MVVM
我一直在创建一个短信应用程序。我有一个存储在 Room
数据库中的对话列表 ConversationEntity
.
这是我的查询:
@Query("SELECT * FROM conversation_entity ORDER BY timestamp DESC")
fun getAllConversations(): Flow<List<ConversationEntity>>
我想在我的存储库 class 中观察(收集)来自此查询的数据,但我必须将其映射到 List<Conversation>
。我知道如何收集这些数据,我知道将 List<ConversationEntity>
映射到 List<Conversation>
。但我不知道应该如何发出对话列表?
我试过从第一个流发出第二个流,或者使用 MutableStateFlow 并通过 .value 设置日期
希望我正确理解了你的问题。如果你想收集 getAllConversations()
然后从它发出另一个流,同时将它映射到另一个流,你可以简单地调用 function.collect { } 然后发出它。
由于您没有提供太多代码库,这里有一个例子:
val myTestFlow: Flow<Int> = flowOf(1,2,3,4,5,6,7,8)
fun myNewFlow(): Flow<String> = flow {
myTestFlow.collect { myInt ->
emit(myInt.toString())
}
}
我还是不明白你的意思,因为你说你知道如何收集 Flow 以及如何将 List 映射到 List。所以无论如何让我试一试:
class DAO {
@Query("SELECT * FROM conversation_entity ORDER BY timestamp DESC")
fun getAllConversations(): Flow<List<ConversationEntity>>
}
class Repository(private val dao: Dao) {
fun getConversations(): Flow<List<Converstaion>> {
// this maps every emitted element of the flow
return dao.getAllConversations.map { list: List<ConversationEntity> ->
// and this maps every element in the list
list.map { conversationEntity ->
conversationEntity.mapToConversation()
}
}
}
}
class ConversationMapper {
// Maps ConversationEntity to Conversation
fun ConversationEntity.mapToConversation(): Conversation {
// I have no idea of the fields, so you have to implement this mapping function yourself.
return Converation(...)
}
}
就是这样。以下是如何在 ViewModel 中使用它:
class YourViewModel : ViewModel(private val repository: Repository) {
val converstationLiveData: LiveData = repository.getConversations().toLiveData()
}
Hope that helps you. But if this is still not what you meant, then please update your question accordingly.
我一直在创建一个短信应用程序。我有一个存储在 Room
数据库中的对话列表 ConversationEntity
.
这是我的查询:
@Query("SELECT * FROM conversation_entity ORDER BY timestamp DESC")
fun getAllConversations(): Flow<List<ConversationEntity>>
我想在我的存储库 class 中观察(收集)来自此查询的数据,但我必须将其映射到 List<Conversation>
。我知道如何收集这些数据,我知道将 List<ConversationEntity>
映射到 List<Conversation>
。但我不知道应该如何发出对话列表?
我试过从第一个流发出第二个流,或者使用 MutableStateFlow 并通过 .value 设置日期
希望我正确理解了你的问题。如果你想收集 getAllConversations()
然后从它发出另一个流,同时将它映射到另一个流,你可以简单地调用 function.collect { } 然后发出它。
由于您没有提供太多代码库,这里有一个例子:
val myTestFlow: Flow<Int> = flowOf(1,2,3,4,5,6,7,8)
fun myNewFlow(): Flow<String> = flow {
myTestFlow.collect { myInt ->
emit(myInt.toString())
}
}
我还是不明白你的意思,因为你说你知道如何收集 Flow 以及如何将 List 映射到 List。所以无论如何让我试一试:
class DAO {
@Query("SELECT * FROM conversation_entity ORDER BY timestamp DESC")
fun getAllConversations(): Flow<List<ConversationEntity>>
}
class Repository(private val dao: Dao) {
fun getConversations(): Flow<List<Converstaion>> {
// this maps every emitted element of the flow
return dao.getAllConversations.map { list: List<ConversationEntity> ->
// and this maps every element in the list
list.map { conversationEntity ->
conversationEntity.mapToConversation()
}
}
}
}
class ConversationMapper {
// Maps ConversationEntity to Conversation
fun ConversationEntity.mapToConversation(): Conversation {
// I have no idea of the fields, so you have to implement this mapping function yourself.
return Converation(...)
}
}
就是这样。以下是如何在 ViewModel 中使用它:
class YourViewModel : ViewModel(private val repository: Repository) {
val converstationLiveData: LiveData = repository.getConversations().toLiveData()
}
Hope that helps you. But if this is still not what you meant, then please update your question accordingly.