4

When using GreenDao and enabling StrictMode i get the following DiskReadViolation:

11-10 12:17:55.777 28694-28694/? D/StrictMode: StrictMode policy violation; ~duration=80 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=589855 violation=2
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1263)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at libcore.io.BlockGuardOs.stat(BlockGuardOs.java:292)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at java.io.File.isDirectory(File.java:522)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at android.app.ContextImpl.validateFilePath(ContextImpl.java:1931)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:566)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:269)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at ltaps.app.onvinyl.Application.BaseApplication.setupDatabase(BaseApplication.java:267)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at ltaps.app.onvinyl.Application.BaseApplication.onCreate(BaseApplication.java:98)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1013)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4707)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at android.app.ActivityThread.-wrap1(ActivityThread.java)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at android.os.Looper.loop(Looper.java:148)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at java.lang.reflect.Method.invoke(Native Method)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-10 12:17:55.777 28694-28694/? D/StrictMode:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-10 12:17:55.777 28694-28694/? D/StrictMode: StrictMode policy violation; ~duration=75 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=589855 violation=2

The setupDatabase function looks like this:

AppOpenHelper helper = new OnAppOpenHelper(this, "app", null);
SQLiteDatabase database = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(database);
daoSession = daoMaster.newSession();

My question is how to i fix the DiskReadViolation? Is it as simple as running setupDatabase() in a new thread or is there any other way to fix this?

I have looked through the documentation of GreenDao and it doesn't seem to provide any help.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • You can disable StrictMode but its not recommended, try to call it from another thread. – Nanoc Nov 10 '15 at 11:55
  • When running the setupDatabase() in a new Thread(...).start(), the DiskReadViolation goes away. I simply want to know if this is the right way to fix the violation and if it is safe. Furthermore i would like to know if there is some other recommended way. – Christian Leicht Jørgensen Nov 10 '15 at 12:00
  • Sure its the right way, the other way is disabling StrictMode and that its no recommended. – Nanoc Nov 10 '15 at 12:02
  • 1
    @Nanoc ... he did enable "more stric" StrictMode (which checks for disk/local storage IO on main thread) ... *I simply want to know if this is the right way to fix the violation and if it is safe.* yes but you have to know that you can only use daoSession after thread finished (fx by wraping thread with some callback ... or simply use the AsyncTask ...) .... of course you may have same error when you would query/update/delete/insert the data ... – Selvin Nov 10 '15 at 12:23

0 Answers0