Hisan
Hisan

Reputation: 2655

Is it good to keep updating room database with every action or to do it at the end?

I am working on a note taking app and i am storing various images and texts using room persistence. I was wondering if it is better to keep updating the database every time user performs an action (like update the existing note) or is it better to do it at the end when the activity terminates or moves on to another activity.

What are the pros and cons of both the options.

Is there any better way than that ?

Upvotes: 0

Views: 378

Answers (3)

Squti
Squti

Reputation: 4487

The best practice is to update your database on every change. If you worry about performance it doesn't have a sensible impact on your app performance. In applications with hundreds of database transactions per second like multipart download managers maybe it impacts the CPU and disk usage but your task is to slow for performance issues. Although you could use onStop() method to save data on the database before the user exit based on Android document but this work has no efficiency in your case and Android doesn't guarantee to call onStop in every situation.

Upvotes: 2

nfl-x
nfl-x

Reputation: 497

IMHO one important thing is to never assume the users just stay within same app for more than 5 minute. They will switch to another app or do lock screen then leave your app for 30 minute and demand same exact screen when they come back to your app. If the activity already cleared by OS then... you know the rest.

So I'll definitely go with the first approach(update db for every action).

Upvotes: 2

Kiryl Tkach
Kiryl Tkach

Reputation: 3634

In my opinion you should update it on every action. Looks like this way google keep works to provide syncronization with server. And you should consider that your app can be closed without calling even onPause (e.g. due to some system crash), so this approach is more safe. So I don't really see any cons in updating database on every action.

Upvotes: 2

Related Questions