I’m working on a function that is currently doing this:
class ConversionProcess: CoroutineScope // + some irrelevant stuff
fun start(): Job {
check(_state.compareAndSet(Stage.NOT_STARTED, Stage.STARTING)) {
"The conversion has already started!"
}
return launch {
val mustDelete: Boolean
val fromDir = if (
fromFile.isFile
&& it.endsWith(".zip") }
) {
_state.value = Stage.EXTRACTING
mustDelete = true
withContext(Dispatchers.IO) {
createTempDir("unzip").also {
deZip(it, fromFile)
}
}
} else {
mustDelete = false
fromFile
}
try {
_state.value = Stage.PARSING_LEVEL_DATA
val levelData = with(LevelDataIO) {
readLevelData(fromDir.resolve("level.dat"))
}.await().let {
_state.value = Stage.DETECTING_EXTRA_INFORMATION
fillMissingInformation(it)
}
val fromStorageEngine = fromStorageEngine
?: levelData.storageEngineType?.default
?: fail("Could not detect the storage engine of $fromFile")
_state.value = Stage.PREPARING_WORLDS
val loadWorldTask = async {
fromStorageEngine.loadWorld(fromDir, levelData)
}
val prepareTargetTask = async {
toStorageEngine.prepareToReceive(toFile, loadWorldTask.await().levelData)
}
} catch (e: Throwable) {
_state.value = Stage.FAILED
throw e
} finally {
if (mustDelete) {
fromDir.deleteRecursively()
}
}
}
It is currently fine, without errors or warning, but it is starting to get too big, so I wanted to move the content inside the try block to its own method. So I extracted a function, but now I get a warning.
fun start(): Job {
//...
try {
process(fromDir)
} catch (e: Throwable) {
_state.value = Stage.FAILED
throw e
} finally {
if (mustDelete) {
fromDir.deleteRecursively()
}
}
}
}
private suspend fun process(fromDir: File) {
_state.value = Stage.PARSING_LEVEL_DATA
val levelData = with(LevelDataIO) {
readLevelData(fromDir.resolve("level.dat")) // Ambiguous coroutineContext due to CoroutineScope receiver of suspend function
}.await().let {
_state.value = Stage.DETECTING_EXTRA_INFORMATION
fillMissingInformation(it)
}
val fromStorageEngine = fromStorageEngine
?: levelData.storageEngineType?.default
?: fail("Could not detect the storage engine of $fromFile")
_state.value = Stage.PREPARING_WORLDS
val loadWorldTask = async { // Ambiguous coroutineContext due to CoroutineScope receiver of suspend function
fromStorageEngine.loadWorld(fromDir, levelData)
}
val prepareTargetTask = async { // Ambiguous coroutineContext due to CoroutineScope receiver of suspend function
toStorageEngine.prepareToReceive(toFile, loadWorldTask.await().levelData)
}
}
What is the best way to solve the warning? I’m confused. I need to suspend so the try-catch-finally works but I also need to launch async tasks there