简体   繁体   中英

LiveData observer is triggered during runInTransaction() in Room database

I use runInTransaction() on my Room database where I update several different tables. I have LiveData observers on those tables that update the UI. What happens is that the observer is triggered in the middle of the runInTransaction() so the UI is updated. But I don't want this. I want the entire transaction to finish and then get a trigger to the observer for the live data.

Instead I get many triggers to the observer in the fragment because I update many different rows in the tables. This causes the UI to "flicker" when data is added and updated in the database. I want an atomic update of the UI when all database operations are done.

Is this possible with the Room and LiveData architecture?

I found the problem. I have two databases. And used the wrong one to call runInTransaction(). The outcome was that everytime I did update or insert it checked if its database was in a transaction and as the wrong database was in a transaction it always triggered the LiveData observer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM