Zoltan Demant
02/18/2025, 3:06 PMsuspend and runBlocking? Looking at the source code, it just seems like a quick-fix to make things work, but I may have missed something. Code in 🧵Zoltan Demant
02/18/2025, 3:10 PMsuspend fun getString(resource: StringResource): String =
loadString(resource, DefaultResourceReader, getSystemResourceEnvironment())
⚠️ This on the other hand, seems awkward:
@Composable
fun stringResource(resource: StringResource): String {
val resourceReader = LocalResourceReader.currentOrPreview
val str by rememberResourceState(resource, { "" }) { env ->
loadString(resource, resourceReader, env)
}
return str
}
Because rememberResourceState looks like this:
@Composable
internal actual fun <T> rememberResourceState(
key1: Any,
getDefault: () -> T,
block: suspend (ResourceEnvironment) -> T
): State<T> {
val environment = LocalComposeEnvironment.current.rememberEnvironment()
return remember(key1, environment) {
mutableStateOf(
runBlocking { block(environment) }
)
}
}
The parsing stuff being suspend makes sense to me, but it seems to be invalidated by the fact that in compose its wrapped in runBlocking?
It kind of reminds me of how the old SharedPreferences used to work, suspend didnt exist at the time and at least a few times, Id do IO operations on the main thread without really knowing about it. Seems like a similar case here (in compose only though)?Zoltan Demant
02/18/2025, 3:18 PMcomposeResources into my own formatting system, where the format call itself is not suspending 😞 I can make it suspend, but Im not sure theres a real benefit if theres just a runBlocking call happening under the hood.Brill
02/20/2025, 8:32 PM