3

I am using strict mode for android 3.2 and am getting StrictModeDiskReadViolation during onCreate in my Activity.

I tried to moved the code that does an SQL query to:

  • a new Thread.
  • a new AsyncTaskLoader.
  • a new AsynTask.

The problem is only AsyncTask made the Violation dissappear and I'm wondering why the other two methods didn't work?

Here is my code:

AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    Dao<Listino, Integer> dao = DatabaseHelper.getHelper(ListinoActivity.this).getListinoDao();
                    if (dao.countOf() == 1)
                    {
                        long id = dao.queryForAll().get(0).getId();//long non int
                        final Intent intent = new Intent(ListinoActivity.this, ListinoProdottiActivity.class);
                        intent.putExtra("listino_id", id);
                        intent.setAction(Intent.ACTION_MAIN);
                        ListinoActivity.this.finish();
                        ListinoActivity.this.startActivity(intent);
                    }
                } catch (SQLException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            MyToast.makeText(ListinoActivity.this, "Errore ottenere i listini", MyToast.LENGTH_SHORT).show();
                        }
                    });

                    e.printStackTrace();
                }
                return null;
            }

        };
        asyncTask.execute();

        AsyncTaskLoader async = new AsyncTaskLoader(this) {


            @Override
            public Object loadInBackground() {
                //do stuff, violation still here
                return null;
            }
        };

        async.loadInBackground();

        Thread t = new Thread() {
            @Override
            public void run() {
                super.run();
                //do stuff, violation still here

            }
        };
        t.run();
ScouseChris
  • 4,377
  • 32
  • 38
max4ever
  • 11,909
  • 13
  • 77
  • 115

1 Answers1

3

You did not fork a Thread. To fork a Thread, you call start(). You called run(), which simply ran your run() method on the current thread.

And you did not come even close to using the Loader framework properly. The code you have there not only suffers from the same flaw as what you did with your Thread, but that is not how you use a Loader.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    I think `Thread.run()` needs to be marked deprecated to avoid this problem. It could be replaced by a protected method that can be overridden, or the entire idea of subclassing `Thread` could be deprecated in favour of using `Runnable`s. Too many people get seriously confused by this, and it's easy for even an experienced professional to make the mistake from time to time... – Jules Mar 12 '13 at 17:18