https://kotlinlang.org logo
#compose
Title
# compose
a

amar_1995

07/12/2020, 7:45 AM
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

07/12/2020, 3:15 PM
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_()
a

amar_1995

07/12/2020, 3:25 PM
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 ?
t

Timo Drick

07/12/2020, 5:43 PM
You could add the data to a ModelList which is also observed by compose.
a

amar_1995

07/13/2020, 3:26 AM
I am getting
Not in frame
error on any element updated.
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

07/13/2020, 10:00 AM
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
a

amar_1995

07/13/2020, 10:13 AM
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

07/13/2020, 10:22 AM
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
a

amar_1995

07/13/2020, 10:51 AM
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

07/13/2020, 10:59 AM
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
a

amar_1995

07/13/2020, 11:42 AM
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