in my fragment
lifecycleScope.launch {
viewModel.capture().collectLatest {
adapter.submitData(it)
}
}
Now when I update the item in Recyclerview
.
repository.update(bean)
db.withTransaction {
db.dao().update(bean)
service.update(requestBody)
}
Because I use paging3 with remoteMediator, like: In dao:
@Query("SELECT * FROM content ORDER BY ... ")
fun getAll(): PagingSource<Int,Content>
In viewmodel:
fun capture() = repository.captureYiMaFlow()
In repository:
fun capture()=
Pager(
config = PagingConfig(pageSize = 6),
remoteMediator = SettingMediator(service, db)) {
db.dao().getAll()
}.flow
The room will auto change the item data in recyclerview.So the data in database will be update.
Becaue I call the service.update(requestBody)
so the data in server will be update.
But the UI of recyclerview doesn't change. I need to close the fragment and open again, then the UI will change.(note that the data of recyclerview have been change. For example:the data of first item of recyclerview is 100
, and I update it to 1000
,then when I click the item ,the data is 1000
, But the UI data doesn't change, it is 100
...
Although I can use adapter.notifyDataSetChange()
to update the UI,I think there is muat some other ways to finish it