J
J
JohnDaniels2016-01-17 21:36:33
Android
JohnDaniels, 2016-01-17 21:36:33

Why is The application may be doing too much work on its main thread if "much work" is done in a separate thread?

The application has a function for updating the database from the server, in which json is parsed and written to the database. This business is performed for 4-5 seconds, so you need to make a progress indicator.
Settled on a solution with ProgressDialog.
First did something like this

public class AssetListActivity extends ActionBarActivity {
     //код неполный
     public boolean onOptionsItemSelected(MenuItem item) {
          try {
                String res = new ClientAsset(mDBConnector).execute(language).get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

            this.recreate();
            return true;
            return super.onOptionsItemSelected(item);
     }

public class ClientAsset extends AsyncTask<String, Void, String> {

       @Override
        protected void onPreExecute() {
            Indicator.setMessage("прогресс");
            Indicator.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            Indicator.setProgress(0);
            Indicator.setMax(100);
            Indicator.show();
           //до обновления индикатора пока не дошел
        }

        @Override
        protected String doInBackground(String... params) {
             //тут долгая операция
        }
}
}

I run - no errors, but progress appears after completion doInBackground
in the log the following:
I/Choreographer﹕ Skipped 413 frames! The application may be doing too much work on its main thread.
found that "means that your application is blocking the main thread for too long."
But why does AsyncTask block the main thread?
the first thing that came to mind was to put the indicator code into the click handler, that is
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
 
            Indicator.setMessage("прогресс");
            Indicator.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            Indicator.setProgress(0);
            Indicator.setMax(100);
            Indicator.show();

            try {
                String res = new ClientAsset(mDBConnector).execute(language).get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

            this.recreate();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

But the problem remained. Why, because now the indicator and updating the database are in different threads? or I don't understand something?
PS also AsyncTask doesn't work onPostExecute, AndroidStudio says there is no such method at all.
d4b4201273f9480ea7eba024a18c7c7a.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FoxInSox, 2016-01-17
@JohnDaniels

Why are you calling your AsyncTask's get method? It blocks your UI thread. You must call execute and wait for the result in onPostExecute. There is an example in the documentation , no need to reinvent the wheel.
AndroidStudio says everything correctly: there is no onPostExecute(Void) method, in your case there is onPostExecute(String). Read about generics.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question