- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 ViewModel 中,我发出 API 请求并使用 StateFlow
和 SharedFlow
与 fragment 通信。在发出 API 请求时,我可以轻松地更新状态流的值并成功收集到 fragment 中。
但在发出请求之前,我使用 SharedFlow
发出了一些 bool 值,并且它没有被收集到 Fragment 中。谁能帮我看看为什么会这样?
class MainViewModel: ViewModel() {
private val _stateFlow = MutableStateFlow(emptyList<Model>())
val stateFlow = _stateFlow.asStateFlow()
private val _loading = MutableSharedFlow<Boolean>()
val loading = _loading.asSharedFlow()
suspend fun request() {
_loading.emit(true)
withContext(Dispatchers.IO) {
/* makes API request */
/* updates _stateFlow.value */
/* stateFlow value is successfully collected */
}
_loading.emit(false) // emitting boolean value
}
}
class MyFragment : Fragment(R.layout.fragment_my) {
// ...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
lifecycleScope.launchWhenStarted {
viewModel.request()
/* stateFlow is collected and triggered - working properly */
viewModel.loading.collectLatest { // <- NOT COLLECTING - WHY?
Log.d(this::class.simpleName, "onViewCreated: $it") // <- NOT LOGGING
}
}
}
}
最佳答案
SharedFlow
是一个热门流。也许你应该创建它
MutableSharedFlow(
replay = 0,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
extraBufferCapacity = 1
)
或
MutableSharedFlow(
replay = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST
)
关于android - SharedFlow 不从发射中收集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71734781/
我正在尝试使用 DynamicMethod 并尝试使用 IL 来创建一些对象。我想创建以下非常基本的对象: new Queue(new List{100}); 我已经使用 ILDASM 查看生成此代码
这是我stackoverflow的第一个问题!我有一个显示 mpl 图 Canvas 的 PyQT gui。我已将主轴周围的边距设置为 0,因此绘图将完全填满图形 Canvas 和包含它的小部件。问题
我的环境是 Windows 7,安装了 scala 2.11.4(运行良好),Java 1.8 我已经尝试过 spark-1.2.0-bin-hadoop2.4 和 spark-1.2.1-bin-h
对于我的生活,我无法弄清楚为什么我不能发送或捕获一些数据。 toggleNavigation() 触发,但我不确定 .emit() 是否真的在工作。 最终我想折叠和展开导航,但现在我只想了解如何将数据
我试图在 VUE 3 中传递一个 emit prop,每次传递它时我仍然得到 false,并且 prop 无法切换。 Accordion .vue
我有一个 View 模型,它采用初始 ViewState对象并具有可公开访问的 state可以收集的变量。 class MyViewModel(initialState: ViewState) : V
现在在玩 RxJava,偶然发现了以下问题: 我有 2 个不同的流: 带有项目的流 Stream(只有 1 个项目),它发出第一个流的转换信息。 所以基本上我有项目流,我希望所有这些项目与第二个流中的
我有一个 API 登录服务,它使用 http 服务来执行登录逻辑(LoginApiService、login-api.service.ts): login(data: LoginCredentials
我们有微服务架构,我们通过网络进行服务间调用。我们在顶层服务中使用 RxJava,这会导致向底层服务创建大量并行请求。因此,我收到“没有到主机的路由错误”或“连接错误”。为此,我想减慢 RxJava
Vue.component('rating-edit', { template:` {{rating.remark}} Sav
我最近购买了 Dream Cheeky Thunder 导弹发射器,我希望通过我的树莓派来控制它。 使用来自报复的代码(https://raw.githubusercontent.com/codeda
我制作了这段代码来记录发送到我的机器人的 DM: client.on('messageCreate', async message => { if (message.author.bot) r
我需要从服务器代码、路由器/ Controller 上的任何位置发出来自服务器的套接字。我检查了一些线程和谷歌,但没有按预期工作。 app.js var app = require('express'
我是一名优秀的程序员,十分优秀!