MVVMLin icon indicating copy to clipboard operation
MVVMLin copied to clipboard

fragment replace 后livedata重复接收数据问题

Open cjcj125125 opened this issue 2 years ago • 1 comments

您好,很感谢你的库,让我学到了很多东西,但是再使用fragment时候发现replace (共享viewmodel)模式会导致观察者重复收到数据,请问下这个有解决方案吗?

cjcj125125 avatar Jun 23 '22 08:06 cjcj125125

LiveData 是有粘性事件的,共享 ViewModel的时候 ViewModel 的生命周期是跟父级相同的,replace fragment 的时候 会触发 LiveData 粘性事件,可以用 MutableSharedFlow 来替代 LiveData , MutableSharedFlow 的replay 默认是 0 ,是没有粘性事件的

aleyn97 avatar Jun 26 '22 12:06 aleyn97