Android -如何从kotlin流读取值?



我有一个从房间数据库集流。我可以作为livedata来观察这个流程,没有问题。

但是当用户单击按钮时,我也想从这个流中读取最后一个值。我尝试了first()终端流操作符,但它无法编译。你能帮我做点别的吗?

非编译尝试从流中读取:

bd.buttonNext.setOnClickListener {
lifecycleScope.launch {
val episode: Episode? = viewModel.episodeFlow().first()           <=== Compile ERROR
Snackbar.make(bd.root, "episode ${episode?.name}", Snackbar.LENGTH_SHORT).show()
}
}

此流来自ROOM:

@Query("SELECT * FROM Episode WHERE id = :id")
fun getEpisode(id: Long): Flow<Episode?>

存储库:

fun getEpisode(episodeId: Long): Flow<Episode?> = dao.getEpisode(episodeId)

视图模型- Id来自于StateFlow:

fun episodeFlow(): Flow<Episode?> 
= episodeIdStateFlow.flatMapLatest { episodeId ->
repository.getEpisode(episodeId)
}
编译错误:
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public fun <T> Array<out TypeVariable(T)>.first(): TypeVariable(T) defined in kotlin.collections
public inline fun <T> Array<out TypeVariable(T)>.first(predicate: (TypeVariable(T)) -> Boolean): TypeVariable(T) defined in kotlin.collections
public fun BooleanArray.first(): Boolean defined in kotlin.collections
public inline fun BooleanArray.first(predicate: (Boolean) -> Boolean): Boolean defined in kotlin.collections
public fun ByteArray.first(): Byte defined in kotlin.collections
public inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte defined in kotlin.collections
public fun CharArray.first(): Char defined in kotlin.collections
public inline fun CharArray.first(predicate: (Char) -> Boolean): Char defined in kotlin.collections
public fun CharSequence.first(): Char defined in kotlin.text

作为"变通方法",我将情节保存在像这样的实例变量中,但如果可能的话,我希望避免这样做:

var episode: Episode? = null
...
viewModel.episodeFlow().asLiveData().observe(this) { episode ->
this.episode = episode
}
...
bd.buttonNext.setOnClickListener {
Snackbar.make(bd.root, "episode ${episode?.name}", Snackbar.LENGTH_SHORT).show()
}

=================== 更新/解决方案21/01/15 ==================

受beigirad启发的解决方案(见下面他的帖子)使用stateIn:

private val _episodeSF  = episodeFlow().stateIn(viewModelScope, SharingStarted.Eagerly, null)
val episodeSF: StateFlow<Episode?>
get() = _episodeSF
fun episodeFlow(): Flow<Episode?> = episodeIdSF.flatMapLatest { episodeId ->
repository.episodeFlow(episodeId)
}

您可以通过statin扩展将您的流转换为StateFlow,然后使用其value属性来获取最新值。

fun episodeFlow(): StateFlow<Episode?> 
= episodeIdStateFlow.flatMapLatest { episodeId ->
repository.getEpisode(episodeId)
}.stateIn(viewModelScope, SharingStarted.Lazily, initialValue)

你粘贴的代码应该可以工作,因为launch的回调已经在协程中运行,你可以从那里调用suspend函数。

一个非阻塞的解决方案可以是使用LiveDataStateFlow。你可以检查属性value,如果没有发出值,它将是null,或者将包含最后发出的值。

你能提供更多的ViewModel代码吗?

相关内容

  • 没有找到相关文章

最新更新