Best way to freeze and apply when the database does some hard work?

I have a help desk that does the hard work of a transaction once a day. If the user launches the application at this time, I want them to sit until it ends (about 10 seconds or so). What is the best way to do this in all activities? Check if the service is running in the onResume function for each activity? And how do we know when this will be done? Local broadcasts, I guess ...

Best practics?

UPDATE: I managed to pause "onResume" until my service was completed. The problem is that the user still sees a blank screen. I tried progressdialogs and regular dialogs while onResume pauses. But all of them are displayed only when onResume returns, and this defeats the goal. In other words, the "Progress" dialog box does not "appear" immediately after calling "show ()": (

+6
source share
4 answers

Local broadcasts are good.

Do your actions ask the service if it is busy or not, and show the appropriate message to the user before the operation is completed.

Once the service is completed, send her the intention that it will be completed.

+1
source

I would have the flag < I'm busy processing 'in db.

And the application should check at startup, and then lock the application nicely, and then check every x seconds to see the status of the flag. This can be done during normal processing, so each action checks this flag locally and on the server before starting.

Sounds like an easy way to block. So you have to take competition into account (lots of users installing and disabling), possibly using a semaphore-like convention.

+2
source

use AsyncTask in your service to work with a heavy database.

and use General Preferences to save the flag to notify of other activities that work in the process. When flag is true means "Work in progress" and the flag "false" means "Work".

Do something like this:

private class DatabaseWork extends AsyncTask<String, Integer, Long> { protected void onPreExecute () { //Set SharedPref flag = trueto notify other activities that work has been started. } protected Long doInBackground(URL... urls) { //Your database work return false; } protected void onProgressUpdate(Integer... progress) { //if you want to show progress to user } protected void onPostExecute(Long result) { //after complete your work set SharedPref flag = false to notify other activities that work has been finished. } 

}

Now you can check the value of the SharedPref flag for each onCreate or onResume action. If you find true, you can display a progress dialog so that the user can wait.

0
source

The best way I've found is to create a “boot activity” that always loads first and sends the user to the home screen when it would be safe.

0
source

Source: https://habr.com/ru/post/917407/


All Articles