0

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

pnkj
  • 406
  • 5
  • 17
  • as much data is provided and assuming that you implemented the `Remote Mediator` as mentioned in the docs, it does look fine especially with the `collectLatest` operator that I was missing and cause me an issue here: https://stackoverflow.com/questions/67495592/pagination-with-room-not-able-to-merge-flowpagingdatamodel-with-other-flows. I can't really think of any possible solutions by looking at this information, maybe add a github link to your project? – mehul bisht Jul 19 '21 at 10:06
  • @mehulbisht I have solve this problem...... But I also have another problem... – pnkj Jul 19 '21 at 14:24
  • so what is the issue this time? :) – mehul bisht Jul 19 '21 at 14:24
  • I put it on the answer: if I change the order of the two line code, the result is different. `adapter.submitData(it) adapter.notifyDataSetChanged()` – pnkj Jul 20 '21 at 01:45
  • well, can you show what the `submitData` actually does? Is it a method of `DIffUtils` or a custom method that you created? – mehul bisht Jul 20 '21 at 02:52
  • Is it a method of DIffUtils, it isn't a custom method . I just call the submitData in PagingAdapter – pnkj Jul 20 '21 at 05:22
  • `DiffUtils` doesn't require you to call `notifyDataSetChanged` explicitly. Does it work only when you call `notifyDataSetChanged` and not when `DiffUtils` method `submitData` is called? – mehul bisht Jul 20 '21 at 05:24

1 Answers1

0

In fragment. Add adapter.notifyDataSetChanged(),like: Change

lifecycleScope.launch {
    viewModel.capture().collectLatest {
        adapter.submitData(it)
    }
}

to

lifecycleScope.launch {
    viewModel.capture().collectLatest {
        adapter.notifyDataSetChanged()
        adapter.submitData(it)
    }
}

The above code can solve the problem.


However,if I change the order of those two lines of code, it won't work as expected

it means if I use

lifecycleScope.launch {
    viewModel.capture().collectLatest {
        adapter.submitData(it)
        adapter.notifyDataSetChanged()
    }
}

It won't work as expected.....

pnkj
  • 406
  • 5
  • 17