android - LiveData如何获取副本数?
问题描述
我在 ViewPager 中有一个包含多个片段的应用程序。有 RecyclerViews 的数据可能在其他片段中重复。所以我决定在一个存储库中为每个单元使用一个 MutableLiveData(使用唯一的密钥),无需额外的副本
livedata中的数据如下:
class Data {
var id = 0
var type = 0
var name = ""
var onlineStatus = OnlineStatus.GroupOrWithout
var icon: Bitmap: ?= null
}
map[data.id + data.type] = Data()
// when we need this data
fun getData(id: Int, type: Int) : MutableLiveData<Data>? {
val res = map[id + type]
if(res != null) {
return res
}
// or create new one if this fist time when we need it
....
}
但现在我意识到,使用 LiveData 的传统方法仅限于片段的生命周期。优点是在fragment被销毁后销毁未使用的数据。但是这种方式对我来说看起来更复杂,因为另一个片段中的事件可能会丢失,然后即使是相同元素,我也会有不同步的数据。
所以我在想,如果 MutableLiveData 没有来自片段的引用,我可以手动操作和销毁它吗?
可能在 android 中是来自 c++ 的 shared_ptr 的类似物?我真的很喜欢当前的架构,但我不知道如何清理未使用的更多 LiveData
解决方案
推荐阅读
- flutter - 如何在颤动中从其 onTap() 函数更改列表图块的属性?
- python - Spyder 中的 Pylint 在 conda 环境中引发“E401 无法导入”
- javascript - 导航到下一页时如何正确清理打开的菜单
- r - 来自 R 中 QAP Logistic 回归的伪 R^2 度量(sna 包中的 netlogit)
- firebase - 使用模拟器而不是云进行 Firebase 云功能测试
- mongodb - 如何为嵌套文档编写聚合查询?
- ios - 在代码中使用 Info.plist 中定义的键作为常量
- keycloak - 在 keycloak 中组织和验证公司
- python-3.x - MemoryError:无法为形状(725000、277、76)和数据类型 float64 的数组分配 30.4 GiB
- c# - BackgroundService.StartAsync,它应该在启动期间完成还是阻塞?