Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to freeze and application while the database is doing some heavy background work?

I have a background service that does some heavy lifting within a transaction once a day. If the user happens to start the app at this time, I want them to sit tight till it finishes (around 10 secs or so). What's the best way to do this across all activities? Check if the service is running in the "onResume" function of each activity? And how do we know when it's done? Local broadcasts I'm guessing...

Best practices anyone?

UPDATE: I've managed to stall "onResume" till my service is done. The problem is that the user just sees a blank screen till then. I've tried progressdialogs and regular dialogs while onResume is paused. But they all display only when onResume returns and that defeats the purpose. In other words, Progress dialog doesn't "show" immediately after "show()" is called :(

like image 814
Bhagwad Jal Park Avatar asked Jun 05 '12 00:06

Bhagwad Jal Park


2 Answers

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

And the app should check on start up, and then lock the app in a nice way, and then check say every x seconds to see the state of the flag. It could do this during normal processing so every action checks this flag locally and on the server before starting.

Seems like a simple way of locking. So you will have to take contention into account (multiple users setting and unsetting) possibly using a a semaphore type arangement.

like image 90
Preet Sangha Avatar answered Nov 11 '22 00:11

Preet Sangha


Local broadcasts is good.

Have your activities question the service if it is busy or not, and display an appropriate message to the user before finishing the activity.

Once the service is done, have it send an intent that it is finished.

like image 39
Trollbane Avatar answered Nov 10 '22 23:11

Trollbane