Colton Idle
12/23/2019, 12:08 AMfromJson()
to do it's work in the background, but I don't think moshi has a suspend function for this? How would someone typically solve this?
viewModelScope.launch {
//Get from file
try {
val file = File(context.filesDir, "mycache")
val contents = file.readText()
val moshi = Moshi.Builder().build()
val jsonAdapter = moshi.adapter(MyObject::class.java)
val json = jsonAdapter.fromJson(contents)
//DO SOMETHING
} catch (e: Exception) {
} finally {
}
}
octylFractal
12/23/2019, 1:05 AMwithContext(<http://Dispatchers.IO|Dispatchers.IO>) { }
octylFractal
12/23/2019, 1:05 AMfromJson
throwing IOException, as I doubt it does IO. file.readText()
probably does thoughColton Idle
12/23/2019, 1:12 AMwithContext{}
?octylFractal
12/23/2019, 1:48 AMjsonAdapter
does other things with global state that involve IO. I would guess that's not the case thoughDico
12/23/2019, 2:09 AMgildor
12/23/2019, 12:55 PMas I doubt it does IOThis warning is about blocking operation, not about only IO. Json creation is indeed potentially long operation even if it doesn't do any IO, so it make sense and it's not fooled. Wrapping to CPU bound dispatcher is not bad idea for serialization/deserialization
Colton Idle
12/24/2019, 9:16 AMgildor
12/24/2019, 9:22 AMoctylFractal
12/24/2019, 9:40 AM<http://Dispatchers.IO|Dispatchers.IO>
iirc