Android: how to properly wait for the service process to complete

Here's the script:

  • I have 2 actions and one service.
  • The first activity is the browse / search page. The second action displays the search results.
  • Search is always performed against internal SQLite db
  • Periodically (say, daily) db needs to be updated from a remote source, which is a long process.
  • If the user searches during the update, I want to wait for the update to complete when the "Wait" warning is displayed. I do not want to query and display search results until the update is complete.
  • The db update is triggered by AlarmManager and is performed by a service that sets the status "UPDATING" to db during the update.
  • I can easily request a status, but how to wait and periodically re-request a database? I use AsyncTask to process the search results, and my reaction to the knee jerk was to put a loop with wait () in the AsyncTask#doInBackground , but this is dangerous and just does not work, since I do not control the flow of the user interface, so I ending with IllegalMonitorStateException .

What would be the β€œright” way to properly wait (maybe even with a status update) in this case?

PS I put the wait code in Runnable and executed it before I even got to my AsyncTask. It works, for example. Thread.sleep(2000) I'm still not sure how to do this safely. Does anyone have any experience with FutureTask ?

+4
source share
2 answers

If the user performs a search during update I want to wait until the update ends while "Wait" is displayed, a warning about readiness. I do not want to query and display search results until the update is fully completed.

This is your call, but remember that you are creating your own problem. Personally, I would drop this requirement. The user should not be uncomfortable just because the alarm went out.

For example, you can turn off the alarm and turn it on again when activity disappears.

Or, so that the update is performed in such a way that it is atomic (for example, doing the update on a copy of the table and then synchronizing the tables in the transaction) so that the activity can safely access the database while the update is taking place.

What would be the β€œright” way to wait (maybe even with update status) in this case?

Notify the service when the update is in progress, through some kind of callback, or possibly an Intent broadcast. Keep a progress indicator until this happens. This still introduces some troubles of the time, so I just give up this requirement.

+2
source

Thanks to Mark (as always) for the helpful information. Here I will tell you how (in my opinion) the scenario described above:

  • Instead of popping the database, just contact the service and start waiting
  • If you cannot get attached to the service, then it does not work, so there is no need for a monkey with it - just request db and do what you need.
  • When the service is complete, start to wait and process any feedback that the service sends back. These may be interim updates, and then the final indicator that the service is complete.
0
source

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


All Articles