Android PagedList updates

前端 未结 3 1067
失恋的感觉
失恋的感觉 2021-02-13 11:02

My question is how to update item in PagedList?

In my case, there are ListActivity and DetailsActivity. List activity is using Paging component to get posts from network

3条回答
  •  误落风尘
    2021-02-13 12:02

    PagedList is immutable

    I would suggest you do not use the paging library because it assumes that your data is immutable which is almost impossible in a practical use-case.

    You can check it in the official docs. It says:

    If you have more granular update signals, such as a network API signaling an update to a single item in the list, it's recommended to load data from the network into memory. Then present that data to the PagedList via a DataSource that wraps an in-memory snapshot. Each time the in-memory copy changes, invalidate the previous DataSource, and a new one wrapping the new state of the snapshot can be created.

    i.e.,

    You have to fetch the data from the server and then store it into local DB using room library and then whenever you have to update any item, update the item into local DB and in turn room will reflect those changes into the UI(using LiveData).

    But then you have to do the paging from using local DB.
    In case of room, you'll have something like this.

    @Dao
    interface FeedDao {
    @Query("SELECT * FROM feed ")
    fun selectPaged(): DataSource.Factory
    }
    

    But then you have to take care of many more things like :

    what if one entity gets deleted from the remote server. How are we going to notice that?
    And we have to remove it from our local DB also.

    You can read this article to solve this problem. It explains all the problems in paging library and a possible workaround if you really want to use the paging library.

    My suggestion is to use this library and implement your own pagination.

提交回复
热议问题