我有一个简单的存储库SimpleRepository
,它使用翻新从服务器获取数据。数据是一个简单的项目列表或一些项目的详细信息。
现在我有一个编辑屏幕,用户可以更改项目名称。所以在用户点击SaveButton后,简单的rest调用被调用,服务器更新项目。
但列表中的项目没有更新。
回购协议如下所示:
interface SimpleRepo {
fun getAllItems(): Flow<List<Item>>
fun getItem(id: String) : Flow<Item>
fun updateItemName(id: String, newName: String): Flow<Boolean> //true if server changed the value
fun updateItemTitle(id: String, newTitle: String): Flow<Boolean> //true if server changed the value
fun updateItemSubtitle(id: String, newSubtitle: String): Flow<Boolean> //true if server changed the value
fun updateItemDescription(id: String, newDescription: String): Flow<Boolean> //true if server changed the value
}
repo由用例使用,而用例由视图模型使用。
我的问题是:调用updateItemXxxx()
方法后,如何实现SimpleRepo从流中获取新元素?
2条答案
按热度按时间qhhrdooz1#
我找到了一个可行的解决方案。见下文。
关键词是:
因此,短期实施:
643ylb082#
你可以试试这样的
onSuccess、更新、API实现不在这里
我建议将所有更新方法更改为
suspend
,除非您需要获得多个发射由于
getAllItems()
始终返回相同的流,并且您正在向该流写入更新,因此它应该在UI上正确观察元素