StrictMode.detectNetwork / Disk все еще применяется в мире сопрограмм? - PullRequest
0 голосов
/ 14 октября 2018

Я следую этому официальному руководству по сопрограмме , и StrickMode продолжает жаловаться, что чтение / запись диска выполняется в MainThread (isSignedIn использует SharedPreference), даже если я указал его для запуска наDispatchers.IO.

override fun onStart() {
    super.onStart()
    launch {
        val isSignedIn = async(Dispatchers.IO) { viewModel.isSignedIn() }
        val navigationAction = if (isSignedIn.await()) R.id.action_splashFragment_to_mainFragment
        else R.id.action_splashFragment_to_whatIsLoftFragment
        withContext(Dispatchers.Main) { navigateToNextWithDelay(action = navigationAction) }
    }
}
  1. Неужели я что-то сделал не так и заставил viewModel.isSignedIn() не работать в фоновом потоке?

  2. И есликод не является неправильным, означает ли это, что "поскольку сопрограмма фактически не блокирует поток, StrictMode.detectNetwork и Disk не имеют значения"?

Обновление: добавление конфигурации StrictMode и журнала

private fun setupStrictMode() {
    if (BuildConfig.DEBUG) {
        StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder()
                .detectAll()
                .penaltyLog()
                .penaltyDeath()
                .build()
        )
        StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder()
                .detectAll()
                .penaltyLog()
                .penaltyDeath()
                .build()
        )
    }
}

Журнал:

2018-10-14 13:11:36.411 13542-13542/io.github.louistsaitszho.loft E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.github.louistsaitszho.loft, PID: 13542
    java.lang.RuntimeException: StrictMode ThreadPolicy violation
        at android.os.StrictMode$AndroidBlockGuardPolicy.onThreadPolicyViolation(StrictMode.java:1705)
        at android.os.StrictMode$AndroidBlockGuardPolicy.lambda$handleViolationWithTimingAttempt$0(StrictMode.java:1619)
        at android.os.-$$Lambda$StrictMode$AndroidBlockGuardPolicy$9nBulCQKaMajrWr41SB7f7YRT1I.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.os.strictmode.DiskReadViolation
        at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1504)
        at java.io.UnixFileSystem.getBooleanAttributes(UnixFileSystem.java:241)
        at java.io.File.isDirectory(File.java:845)
        at dalvik.system.DexPathList$Element.maybeInit(DexPathList.java:696)
        at dalvik.system.DexPathList$Element.findResource(DexPathList.java:729)
        at dalvik.system.DexPathList.findResources(DexPathList.java:526)
        at dalvik.system.BaseDexClassLoader.findResources(BaseDexClassLoader.java:174)
        at java.lang.ClassLoader.getResources(ClassLoader.java:839)
        at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:349)
        at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:402)
        at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:488)
        at kotlin.collections.CollectionsKt___CollectionsKt.toCollection(_Collections.kt:1062)
        at kotlin.collections.CollectionsKt___CollectionsKt.toMutableList(_Collections.kt:1095)
        at kotlin.collections.CollectionsKt___CollectionsKt.toList(_Collections.kt:1086)
        at kotlinx.coroutines.experimental.MainDispatcherLoader.<clinit>(Dispatchers.kt:97)
        at kotlinx.coroutines.experimental.Dispatchers.getMain(Dispatchers.kt:54)
        at io.github.louistsaitszho.loft.ScopedFragment.getCoroutineContext(ScopedFragment.kt:20)
        at kotlinx.coroutines.experimental.CoroutineContextKt.newCoroutineContext(CoroutineContext.kt:81)
        at kotlinx.coroutines.experimental.BuildersKt__Builders_commonKt.launch(Builders.common.kt:49)
        at kotlinx.coroutines.experimental.BuildersKt.launch(Unknown Source:1)
        at kotlinx.coroutines.experimental.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:46)
        at kotlinx.coroutines.experimental.BuildersKt.launch$default(Unknown Source:1)
        at io.github.louistsaitszho.loft.splash.SplashFragment.onStart(SplashFragment.kt:28)
        at android.support.v4.app.Fragment.performStart(Fragment.java:2372)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1467)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3244)
        at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:3206)
        at android.support.v4.app.Fragment.performStart(Fragment.java:2378)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1467)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3244)
        at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:3206)
        at android.support.v4.app.FragmentController.dispatchStart(FragmentController.java:206)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:605)
2018-10-14 13:11:36.412 13542-13542/io.github.louistsaitszho.loft E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
        at android.app.Activity.performStart(Activity.java:7157)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2937)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
...