从 Java 收集的 SharedFlow 不起作用

问题描述

我有一小组活动需要在后台生成的某些事件上重新启动。 由于这将是一个短期的解决方法,我认为侵入性最小的方法是在存储库中有一个共享流,从 UI 一直观察到,但由于某种原因它不起作用:我可以看到价值发出,但 UI 不会对其做出反应。

我当前的代码

// Repository
val myEventFlow = MutableSharedFlow<Unit>(replay = 0)
 
// Use Case to emit the event (dummy impl,just to test the mechanism is working)
operator fun invoke() = runBlocking {
    myExistingRepository.myEventFlow.emit(Unit)
}
 
// Use Case class to fetch the flow
operator fun invoke() = myExistingRepository.myEventFlow.asSharedFlow()
 
// View Model (in Java,can't convert it)
public LiveData<Unit> myEvent() {
  return FlowLiveDataConversions.asLiveData(getMyEventUseCase.invoke());
}
 
// Activity (also in Java)
viewmodel.myEvent().observe(this,event -> activityHelper.startWithClearStack(getClass()));
 
// Activity Helper (dummy impl just to see the event being reacted to)
fun startWithClearStack(activity: Class<out Activity>) {
    Timber.d("DEBUG: restarting class $activity")
}

我可以确认事件已发出(在调试中,包含 emit() 的行被执行)但随后活动中的观察者从未被激活。 知道为什么吗?

提前致谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)