Moh*_*dar 0 android kotlin kotlin-coroutines kotlin-flow kotlin-sharedflow
我的问题是我们如何从构建的流对象中发出一个值,如下所示:
class Sample {
var flow: Flow<Object> = null
fun sendRequest(){
flow = flow{}
requestWrapper.flowResponse = flow
}
fun getResponse(){
// I want to emit data here with built flow object on
// requestWrapper like this
requestWrapper.flowResponse.emit()
}
}
Run Code Online (Sandbox Code Playgroud)
这个问题有什么可能的解决方案吗?
您可以用来MutableSharedFlow发出像您的情况一样的值:
class Sample {
var flow: MutableSharedFlow<Object>? = MutableSharedFlow(...)
fun sendRequest(){
requestWrapper.flowResponse = flow
}
fun getResponse(){
// I want to emit data here with built flow object on
// requestWrapper like this
requestWrapper.flowResponse?.tryEmit(...)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
627 次 |
| 最近记录: |