architecture-components-samples icon indicating copy to clipboard operation
architecture-components-samples copied to clipboard

Android room database - LiveData paging slow appear when a lot of insert/update into database

Open tvhieuit opened this issue 8 years ago • 20 comments

I'm working in a project realtime with LiveData and Paging database I meet the issue: -- My list take long time to appear when have a lot of insert/update to database.

val viewModel = ViewModelProviders.of(this).get(MyViewModel::class.java) viewModel.init(database) viewModel.getUserList()?.observe(this, Observer { println("load paging :::: ${it?.size}") })

could anybody help me

Thank you so much. Hieu Tran

tvhieuit avatar Dec 27 '17 03:12 tvhieuit

why you close this? have any solution?

MichaelJokAr avatar Feb 08 '18 03:02 MichaelJokAr

currently, I don't have any solution :)

tvhieuit avatar Feb 08 '18 06:02 tvhieuit

I use LiveData to my project without room, I find when I change database the ui update very slow. It's there a good way to solve this? to update sql or about ui code? Thanks

MichaelJokAr avatar Feb 08 '18 08:02 MichaelJokAr

maybe, you load large database to UI? I also meet this issue when I got database realtime (same as chat realtime)

tvhieuit avatar Feb 08 '18 08:02 tvhieuit

I can confirm it, i tried using Room with Livedata and the changes is quite slow.

nooballday avatar Mar 16 '18 06:03 nooballday

Do you have any solution for this?

tvhieuit avatar Mar 16 '18 08:03 tvhieuit

Same problem use Room when large data

crossle avatar Aug 08 '18 06:08 crossle

I think that Room (+LiveData) will re-query for every updates so it's causing slow when have a lot of insert/update

NhatPham94 avatar Sep 06 '18 09:09 NhatPham94

@NhatPham94 I know, but how to fix it :D

tvhieuit avatar Sep 12 '18 07:09 tvhieuit

@tvhieuit Currently I'm working on a demo with Paging lib + Room + LiveData, I trigger updates and try to debug and see what's going on. The result is Room only loads specific number of items follow what I've config with Paging lib (ex : page size) instead of re-create the whole query

NhatPham94 avatar Sep 12 '18 07:09 NhatPham94

@tvhieuit Have you found any solutions ?

NhatPham94 avatar Oct 16 '18 07:10 NhatPham94

Currently, the Paging library will start loading from the DB, and if content in the DB has changed by the time it's done, we won't send the result, and will instead reload.

The behavior is intended to avoid sending too many, too-similar lists to the UI, but we could make it smarter to avoid the problem (send something to avoid spinning continuously). If you file a bug ( https://issuetracker.google.com/issues/new?component=413106&template=1096385 ), I can look into it, especially if you have a sample repro project.

ChrisCraik avatar Oct 16 '18 17:10 ChrisCraik

Hi , Anybody found resolutions for this issue.

ghost avatar May 04 '19 14:05 ghost

Related issue with a possible solution: https://issuetracker.google.com/issues/134685570

M66B avatar Jul 22 '19 09:07 M66B

Hope you have fired query in doAsync{} !!

sidthakkar12 avatar Oct 23 '19 13:10 sidthakkar12

@sidthakkar12 paging is by definition async, so maybe you don't understand this issue.

M66B avatar Oct 23 '19 13:10 M66B

I use this https://realm.io/blog/realm-java-0-84-0/ to insert many data, I was try insert data up to 2Mb and no take long time.

but it take so many size of stored to save that data.

jemmycalak avatar Nov 26 '19 16:11 jemmycalak

I try this code on room and get better time to insert many data

@Dao interface Dao{ @Insert(onConflict = REPLACE) fun insertData(m :List) }

jemmycalak avatar Nov 27 '19 16:11 jemmycalak

I experienced the same issue and i found that i have two updates, one after another, on the same table. The first update was triggering the observer but the changes were not relevant for the query and no results were returned, the second update wasn't triggering the observer immediately because it was busy querying the data after changes from the first update and the event has been postponed. Merging to updates into one query has fixed the issue (if it is not possible you may try using @Transcation )

nikolamin avatar Apr 01 '20 14:04 nikolamin

As Chris mentioned - this seems to be working as intended.

In v3 APIs, we have an open-issue to better support the high frequency updates use-case by allowing a stream-based API for delivering item / page updates without invalidation loop: https://issuetracker.google.com/149482600

Please follow / comment / +1 there!

dlam avatar Feb 10 '21 00:02 dlam