B3nedikt/restring

Strict mode

SiarheiSm opened this issue · 1 comments

Hi, I've noticed there is an issue appears in case I've test app with strict mode:

 Caused by: java.lang.RuntimeException: StrictMode ThreadPolicy violation
        at android.os.StrictMode$AndroidBlockGuardPolicy.onThreadPolicyViolation(StrictMode.java:1768)
        at android.os.StrictMode$AndroidBlockGuardPolicy.handleViolationWithTimingAttempt(StrictMode.java:1630)
        at android.os.StrictMode$AndroidBlockGuardPolicy.startHandlingViolationException(StrictMode.java:1601)
        at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1556)
        at libcore.io.BlockGuardOs.access(BlockGuardOs.java:69)
        at libcore.io.ForwardingOs.access(ForwardingOs.java:73)
        at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:7246)
        at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:281)
        at java.io.File.exists(File.java:815)
        at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:645)
        at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:636)
        at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:592)
        at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:787)
        at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:439)
        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:178)
        at dev.b3nedikt.restring.Restring$init$1.invoke(Restring.kt:60)
        at dev.b3nedikt.restring.Restring$init$1.invoke(Restring.kt:59)
        at dev.b3nedikt.restring.repository.SharedPrefsStringRepository.createSharedPrefsValueSetStore(SharedPrefsStringRepository.kt:69)
        at dev.b3nedikt.restring.repository.SharedPrefsStringRepository.<init>(SharedPrefsStringRepository.kt:27)
        at dev.b3nedikt.restring.Restring.init(Restring.kt:59)

I've enabled mode with :

private fun strictMode() {
        if (isDebug() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            val threadPolicy = StrictMode.ThreadPolicy.Builder()
                .detectAll()
                .penaltyListener(Executors.newSingleThreadExecutor()) {
                    Timber.e(it)
                }.build()
            StrictMode.setThreadPolicy(threadPolicy)

            val vmPolicy = StrictMode.VmPolicy.Builder()
                .detectAll()
                .penaltyListener(Executors.newSingleThreadExecutor()) {
                    Timber.e(it)
                }.build()
            StrictMode.setVmPolicy(vmPolicy)
        }
    }

Yeah, the SharedPrefsStringRepository does reading and opening of the SharedPrefs on the main thread. I don't really think this is an issue as write operations are done with apply, so they are async anyway. And reads are done lazily only the ones which are needed for a view are loaded. I may look into optimizations around moving SharedPrefs handling entirely to the background if I find the time.