Hi all. Using molecule to launch a stateflow like ...
# squarelibraries
x
Hi all. Using molecule to launch a stateflow like this
Copy code
expect fun ViewModel.ViewModelDispatcher(): CoroutineContext
expect val Default: RecompositionClock

// androidMain
actual fun ViewModel.ViewModelDispatcher(): CoroutineContext = AndroidUiDispatcher.Main + SupervisorJob()
actual val Default: RecompositionClock = ContextClock

// iosMain
actual fun ViewModel.ViewModelDispatcher(): CoroutineContext = Dispatchers.Main + SupervisorJob()
actual val Default: RecompositionClock = Immediate

abstract class ViewModel: CoroutineScope {
  override val coroutineContext: CoroutineContext = ViewModelDispatcher()
  override fun onDestroy() { coroutineContext.cancel() }
}

class MyScreenViewModel : ViewModel() {
  val states: StateFlow<MyState> = launchMolecule(Default) { MyPresenter() }
}
This works as expected on Android. However, on iOS this throws the following
Copy code
Uncaught Kotlin exception: kotlin.IllegalStateException: Current context doesn't contain Job in it: [app.cash.molecule.GatedFrameClock@ba2c2b0, MainDispatcher]
Am I missing something obvious here? 🤔