StrictMode giving warning when I collect from Coroutines Flow?

Viewed 51

Can somebody please explain why StrictMode giving me warning when I collect in Main thread?

I have strictMode enabled in my application class file.

StrictMode.setThreadPolicy(
            StrictMode.ThreadPolicy.Builder()
                .detectDiskReads()
                .detectDiskWrites()
                .detectNetwork() 
                .penaltyLog()
                .build()
        )

Here I launching a coroutine to collect from the Flow.

binding.buttonOne.setOnClickListener {
    CoroutineScope(Dispatchers.Main).launch {
       simple().flowOn(Dispatchers.IO).collect {
             Log.d(TAG, "in collect block: ${Thread.currentThread().name}")
             binding.flowExampleone.text="$it"
             log(it.toString())
         }
    }
}
 fun simple() = flow<Int> {
        for (i in 1..3) {
            Log.d(TAG, "in simple block: ${Thread.currentThread().name}")
            delay(1000)
            emit(i)
        }
    }

    fun log(message: String) {
        Log.d("mytag", message)
    }

But when i run it. The StrictMode is giving warning.

D/StrictMode: StrictMode policy violation; ~duration=34 ms: android.os.strictmode.DiskReadViolation
0 Answers
Related