Comments (11)
not, actually
GlobalScope.launch { // any scope/context, it does not matter
val machineScope = CoroutineScope(newSingleThreadContext("your context")) // must be single threaded, you should cancel it when you complete working with the machine.
val machine = createStateMachine(machineScope, ....) {...} // this scope/context matters
//...
// the library will execute all suspendable functions from machineScope context
machine.processEvent(...) // will switch to machineScope internally, so you can call it from any thread and context.
// please not that it is only about suspendable library functions. all other non-suspendable api cannot be used in
// multithreaded environment without proper synchronization.
}
from kstatemachine.
Hi, could you provide a code sample facing this issue?
from kstatemachine.
Is the falling StateMachine called from multiple threads? Pease specify with which scope do you call createStateMachine function.
from kstatemachine.
Is the falling StateMachine called from multiple threads? Pease specify with which scope do you call createStateMachine function.
@nsk90 , thank you very much for replying.
I called createStateMachine
from GlobalScope.launch{}
.
from kstatemachine.
@nsk90
for now I have made following modification and so far it did not crash. But not sure if it is the right way to do
private val SINGLE_THREAD = newSingleThreadContext("mvi")
fun sendEvent(event: AppEvent): Unit = intent {
sendEffect(ModelEffect.ControlEventSent(event))
val machine = NysnoApplication.getApp().machine
GlobalScope.launch {
withContext(SINGLE_THREAD){
machine?.processEvent(event)
}
}
}
from kstatemachine.
I think the crash is related to GlobalScope
usage. As it uses Default coroutine dispatcher internally (correct?). This dispatcher uses thread pool, not a single thread. So processEvent
may be executed concurrenly, which is wrong. You should pass single threaded coroutine context into createStateMachine
function.
Try it. If this does not help, I will try to reproduce your issue locally to debug.
from kstatemachine.
@nsk90
is this fine?
GlobalScope.launch {
withContext(singleThread){
machine = createAndStartStateMachine()
machine?.start(null)
printStateMachine()
}
}
from kstatemachine.
yeah this looks better, please inline createAndStartStateMachine()
body, so is can see pure library methods, to give exact answer.
I think you don't need GlobalScope
at all.
Having separate single thread you can create your own CoroutineScope
from it, and use the latter as createStateMachine
argument.
This is not critical and does not change the way how it should work, both cases are effectively equivalent.
Something like this:
CoroutineScope(newSingleThreadContext("your context"))
// or
CoroutineScope(Dispatchers.Default.limitedParallelism(1))
from kstatemachine.
this is how createAndStartStateMachine
looks. Actually it took lot of time to create if I pass start = true
while creating and I got machine
variable not initialized error in sendEvent
function while accessing machine
, so had to change it this way.
suspend fun createAndStartStateMachine(): StateMachine {
return createStateMachine(GlobalScope, "MyAppState", ChildMode.PARALLEL, start = false) {
logger = StateMachine.Logger {
Timber.tag("StateMachine").d(it())
}
state("AppState") {
addInitialState(AppState.LoginState) {
....
}
...
}
}
}
from kstatemachine.
CoroutineScope(Dispatchers.Default.limitedParallelism(1))
you mean like this?
val applicationScope = CoroutineScope(Dispatchers.Default.limitedParallelism(1))
applicationScope.launch {
machine = createAndStartStateMachine()
machine?.start(null)
printStateMachine()
}
from kstatemachine.
not, actually
GlobalScope.launch { // any scope/context, it does not matter val machineScope = CoroutineScope(newSingleThreadContext("your context")) // must be single threaded, you should cancel it when you complete working with the machine. val machine = createStateMachine(machineScope, ....) {...} // this scope/context matters //... // the library will execute all suspendable functions from machineScope context machine.processEvent(...) // will switch to machineScope internally, so you can call it from any thread and context. // please not that it is only about suspendable library functions. all other non-suspendable api cannot be used in // multithreaded environment without proper synchronization. }
thank you very much @nsk90
from kstatemachine.
Related Issues (20)
- onEntry with Transition or Conditional Transition without Event HOT 5
- Update Data of a DataState HOT 4
- data transition should no be self targeted, use simple transition instead HOT 2
- please add choiceDataState method HOT 3
- java.lang.IllegalArgumentException: Cannot set current state in child mode PARALLEL HOT 1
- State machine and Android lifecycle HOT 6
- Export to PlantUml: GuardedTransitionOnBuilder are not drawn in diagram HOT 4
- Sources are not packaged correctly HOT 2
- Versions HOT 3
- State, transition names HOT 10
- Getting crash if I try to use `machine.startBlocking` from machine created with `createStdLibStateMachine` HOT 3
- Support wasmJs HOT 6
- Clarification of concurrency HOT 5
- Event queue is not empty, internal error HOT 5
- Need explanation for choice states HOT 3
- Data validation and transformation via events or state HOT 4
- How to do parallel data states? HOT 2
- Initial Choice State as Nested State of Data State HOT 3
- Implement Intellij visualisation plugin
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kstatemachine.