Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android room database is not inserting data in db immediately

I was using room in my android project, everything was working fine until one day I found Room doesn't insert data in db immediately.

How I reproduced it

When I run command appdatabase.getUserDao().insert(user) and after 2 seconds when i remove my battery from my phone and export db after that What I found is data is not inserted in db. that is 100% sure because my team has reproduced it multiple times. it seems instead of directly store into db it caches data for some time.

Is there any solution for it Where data will directly store in db instead it caches?

like image 290
Tarun Avatar asked May 16 '26 15:05

Tarun


1 Answers

Yes, close the database before exporting:

RoomDatabase.close()

Make sure it is the

Room.databaseBuilder()

and not

Room.inMemoryDatabaseBuilder()

Check if the thread is executing properly and that you are not interrupting a @Transaction insert

like image 138
zjmo Avatar answered May 19 '26 05:05

zjmo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!