amar_1995

    amar_1995

    2 years ago
    I have implementated a infinite scroller which loads data at the end of page. And it is working as expected. But I also want to keep observing all loaded data for any changes from local database. I don't know how to do this. Any suggestion ?
    n

    nglauber

    2 years ago
    I think you could use LiveData/Flow to observe your database (or maybe, Paging library once you’re working with paged data). Then you set this data to your Composable state using
    yourLiveData._observeAsState_()
    amar_1995

    amar_1995

    2 years ago
    Yes I am doing for 1st chunk of data. But after the scroll down there will be 2nd chunk of data and so on. I am only able to observe last chunk of data. But don't know how to observe the loaded previous chunks.
    Is there any way to observe all loaded small chunk of data using livedata or something ?
    Timo Drick

    Timo Drick

    2 years ago
    You could add the data to a ModelList which is also observed by compose.
    amar_1995

    amar_1995

    2 years ago
    I am getting
    Not in frame
    error on any element updated.
    m

    molikto

    2 years ago
    I once created a partial solution, where the query result is cached and is automatically & incrementally updated using the change-data. I am recently thinking properly implement it, most importantly adding support for join and subqueries. I think it is kind of https://materialize.io/ but for client side apps. Is there a need for this?
    amar_1995

    amar_1995

    2 years ago
    Room database will trigger any data change if observing a list of data as livedata or flow. But I am working infinite scroll to show list of data. Now I am only able to observe latest chunk of data but unable to observe loaded data.
    m

    molikto

    2 years ago
    As I understand your queries is paginated using for example
    LIMIT
    , so you don't load too much data into memory? and you have two query one for first page and then one for load more? maybe you can use
    @Query
    and combine with
    InvalidationTracker.Observer
    API instead, then create your own
    Observable
    .
    (the approach I was talking about is incrementally update the query result without requery the database, because you know what the change is, so you know how it will change the query result. this can avoid requery if the dataset is very large. but it is error-prone to implement it manually
    amar_1995

    amar_1995

    2 years ago
    Yes, I limit the data using
    LIMIT
    and getting the list of data as livedata from room query.
    Now to load more I am using same query with a dynamic limit and offset value
    I am using server sent events which will update room local database
    I want to reflect those change in ui
    m

    molikto

    2 years ago
    I think in this case the
    @LiveData
    is not a useful abstraction, because the query doesn't conrespond to a list of items that can be directly shown to users. it is better to use the two query as one-off
    @Query
    , and construct your own
    LiveData
    instead
    amar_1995

    amar_1995

    2 years ago
    So you mean get a list of data(not livedata) from query as
    @Query
    and then construct my own
    LiveData
    of that list ?
    But the issue is how to know if any data changes is that livedata?
    m

    molikto

    2 years ago
    I think you can use https://developer.android.com/reference/androidx/room/InvalidationTracker.Observer . It seems to me that's what LiveData adapter and Observable adapter use under the hood
    amar_1995

    amar_1995

    2 years ago
    I need to check this
    I am not getting how to use
    InvalidationTracker.Observer
    . Due you have any example. It will be a great help