'File.createTempFile() shows "Inapropriate blocking method call" in CoroutineWorker

I have the following class that implements WorkManager

@HiltWorker
class LexemeWorker @AssistedInject constructor(
    @Assisted appContext: Context,
    @Assisted workerParams: WorkerParameters,
    private val firebaseStorage: FirebaseStorage,
) : CoroutineWorker(appContext, workerParams) {

    companion object {
        const val Progress = "Progress"
        private const val delayDuration = 1L
    }

    override suspend fun doWork(): Result {

        val storageRef = firebaseStorage.reference

        val pathReference = storageRef.child("lexeme.zip")

        val file = File.createTempFile("lexeme", ".zip")

        val path = pathReference.getFile(file).await()

        var progress = 0.0

        while (progress < 100) {
            progress = (100.0 * path.bytesTransferred) / path.totalByteCount
            setProgress(workDataOf(Progress to progress))
            delay(delayDuration)

            if (path.error != null) {
                return Result.failure()
            }
        }
        
        return Result.success()
    }
}

I don't know for whatever reason, the android studio is showing the message "Inapropriate blocking method call" on createTempFile. I have also tried to put everything inside withContext(Dispatchers.IO) but without a chance of solving the problem. How to get rid of this warning?

Edit1: The other way around withContext(Dispatchers.IO)

override suspend fun doWork(): Result = withContext(Dispatchers.IO) {

    ....

    val file = File.createTempFile("lexeme", ".zip")

    ...

    while (progress < 100) {
        progress = (100.0 * path.bytesTransferred) / path.totalByteCount
        setProgress(workDataOf(Progress to progress))
        delay(delayDuration)

        if (path.error != null) {
            Result.failure()
        }
    }

    Result.success()
}


Solution 1:[1]

Not sure why this warning occurs, maybe because File.createTempFile() function throws IOException. To get rid of this warning we can wrap File.createTempFile() function into runCatching:

...

val file: File? = runCatching {
    File.createTempFile("lexeme", ".zip")
}.getOrNull()

...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BigSt