Here's scenario:
- I have 2 activities and one service
- First activity is a landing view/search page. Second activity displays search results
- Search is always executed against internal SQLite db
- Periodically (say daily) db needs to be updated from the remote source which is a long process
- If user performs the search during the update I want to wait until the update is over while displaying "Please wait" progress alert. I don't want to query and display search results until refresh is fully done.
- The db update is triggered by AlarmManager and executed by service which puts "UPDATING" status into db while update is in progress
- I can easily query the status but how do I wait and periodically re-query the database? I'm using AsyncTask to process search results and my knee-jerk reaction was to put a loop with wait() into
AsyncTask#doInBackground
method but that's dangerous and simply doesn't work since I'm not controlling the UI thread so I end up withIllegalMonitorStateException
.
What would be a "right" way to properly wait (may be even with status update) in this case?
P.S. I placed the "wait" code into a Runnable and executing it even before I get to my AsyncTask. It works e.g. Thread.sleep(2000)
still I'm not sure that's the way to do it safely. Does anyone have any experience with FutureTask
?