-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use derived without Dispatchers.Main #30
Comments
You can just pass dispatcher = dispatchers.default. We use dispatchers.main by default because many iOS APIs must be called from the main thread and most lower-level APIs like Ktor switch to a different thread when necessary already. In most cases you don't gain anything by using dispatchers.default unless you're doing CPU or IO intensive operations directly on the main thread. In those rare cases you can pass a custom dispatcher. That's still better than your app crashing on iOS. |
Where can I pass dispatcher ? import com.ensody.reactivestate.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
val appScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
val number = MutableStateFlow(5)
val isPrime = appScope.derived {
get(number) % 2 == 0
}
fun main() = runBlocking<Unit> {
launch {
while (isActive) {
number.update { it + 1 }
delay(1000)
}
}
//we are in coroutineScope
autoRun {
println("${get(number)} is ${get(isPrime)}")
}
} when I run this code I get this this exception
|
There are two different kinds of derived: blocking and suspend based. Passing a dispatcher only makes sense for suspend based code. There you can use However, you're trying to run this code on a target that doesn't have a main dispatcher at all. This lib currently requires one, though, and a fix would be needed to make it work at all. |
Thanks BTW, it will be good to support environments with no dispatchers (for example setting Dispatchers.Default as a fallback) |
I have a simple console application that I want to use 'derived' to map some state flows
can I do that without
Dispatchers.Main
?I can't figure out why
derived
needs the Main Dispatcher . (why not usingDispatchers.Default
? by default)The text was updated successfully, but these errors were encountered: