Design template for a service such as architecture

Which design template would be intelligent in which the following Components (simplified) exist:

3 components
- GUI
- Data collection

- Database

Design

I do not have access to a server on the Internet, its just a data source. Data stored on the Internet is always newer, a local database is just a copy (cache) of the one on the Internet. The graphical user interface can request and update the local cache, and then receive asynchronously new data, which may take some time. The GUI only displays data from the local database, which it can synchronize.

So my question is: what classes would you use to possibly run the service with Progressbar features long term? Are there any better projects for this kind of "problem"? Are there any more efficient methods?

+6
source share
1 answer

In the service Like Component:

  • The interface (method) for starting the update process. Typically, this service returns jobId to indicate a job that is being processed in the background.
  • Another interface (method) for obtaining status based on a specific jobId . The implementation of this service may return status, percentCompleted, or any other relevant information that will inform the caller of the current status of the update process. The implementation of this method should be able to report progressive progress (for example, reporting incremental storage in local storage) in order to make an accurate progress bar, otherwise a better user interface might show a progress bar.
    • Please note that if such incremental reporting is not possible to implement, then during the upgrade process you should probably use the typical AsyncTask , which performs the update in the background and tells the user when it will be completed. If this operation may take some time, you can implement the completion of the update through the Android notification bar or push notification.

Assuming you have an interface for getting the upgrade progress, you can use AsyncTask onProgressUpdate to report the progress of the upgrade. This method is specifically designed for this.

Your steps are approximately the following:

  • Perform an interface upgrade through one AsyncTask. Since your update happens asynchronously, this particular task should return pretty quickly with a status indicating whether the execution is successful or unsuccessful, because there is some exception along with the jobId that it is currently executing
  • Launch another AsyncTask that checks for update status and reports progress through onProgressUpdate . AsyncTask is approximately similar to
 public class GetUpdateStatusAsyncTask extends AsyncTask { protected Integer doInBackground(Integer... param) { // this is the jobId that you get from the previous AsyncTask that you use to kick off the // the update process int jobId = param[0]; double percentCompleted = fetchStatus(jobId); while(percentCompleted != 100.00) { publishProgress(percentCompleted); } // return status code return 1; } protected void onProgressUpdate(Double... progress) { // set the progressBar here } protected void onPostExecute(Long result) { // done, handle status code } private double fetchStatus(int jobId) { double percentCompleted = 0.0; // call interface to get the update based on the jobId // return percentCompleted status or make a more complex object if you need // more detail info return percentCompleted; } } 
+1
source

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


All Articles