Can anyone comment on why composeResources use a m...
# multiplatform
z
Can anyone comment on why composeResources use a mixture of
suspend
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 🧵
👀 3
To me, this makes sense:
Copy code
suspend fun getString(resource: StringResource): String =
    loadString(resource, DefaultResourceReader, getSystemResourceEnvironment())
⚠️ This on the other hand, seems awkward:
Copy code
@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:
Copy code
@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)?
Im trying to fit
composeResources
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.
b
@Zoltan Demant I'm having the same issue, writing a datetime formatting library. I would really like toe keep is as simple as possible. In my case the content is light enough I can probably forgo the resources, but its certainly makes things more interesting. It's clear why its like that though. It needs a different way to store resources across so many platforms, so this creates a nice consistent way to do it. I somewhat think it would be better to compile the resources like we do not with icons, but i have not considered that thoroughly enough yet. I will be experimenting with that for my datetime library though.
🙏🏽 1