Search code examples
koinkotlin-native

What's Koin's Behavior when providing dependencies in Kotlin Native?


Since Kotlin Native have a different threading Model, I wonder how Koin 3.0 provides its dependencies in Kotlin Native? Does it behave like a @ThreadLocal where only a copy of the dependency is provided? or is it handled like an Atomic Reference?


Solution

  • Koin running in Kotlin/Native restricts injecting state, or interacting with Koin at all, to the main thread. This allows Koin internals to avoid AtomicReference, and avoid freezing the state you create. However, you can't directly inject from other threads.

    There were a few different designs of the Koin internals for Kotlin/Native. The first version kept everything in frozen state and accessible from all threads, but in practice, for native mobile at least, most injecting happens on the main thread. Keeping Koin on the main thread means it won't freeze your state automatically.

    This design will obviously be relaxed as the memory model changes, but I would not expect the new memory model to be anywhere near production until sometime next year.