Lazy Modules and Background Loading
In this section we will see how to organize your modules with lazy loading approach.
note
Lazy Modules API is stable since 4.0
Defining Lazy Modules
You can now declare lazy Koin module, to avoid trigger any pre allocation of resources and load them in background with Koin start.
lazyModule
- declare a Lazy Kotlin version of Koin ModuleModule.includes
- allow to include lazy Modules
A good example is always better to understand:
// Some lazy modules
val m2 = lazyModule {
singleOf(::ClassB)
}
// include m2 lazy module
val m1 = lazyModule {
includes(m2)
singleOf(::ClassA) { bind<IClassA>() }
}
info
LazyModule won't trigger any resources until it has been loaded by the following API
Background loading with Kotlin coroutines
Once you have declared some lazy modules, you can load them in background from your Koin configuration and further more.
KoinApplication.lazyModules
- load lazy modules in background with coroutines, regarding platform default DispatchersKoin.waitAllStartJobs
- wait for start jobs to completeKoin.runOnKoinStarted
- run block code after start completion
A good example is always better to understand:
startKoin {
// load lazy Modules in background
lazyModules(m1)
}
val koin = KoinPlatform.getKoin()
// wait for loading jobs to finish
koin.waitAllStartJobs()
// or run code after loading is done
koin.runOnKoinStarted { koin ->
// run after background load complete
}
note
The `lazyModules` function allow you to specify a dispatcher: `lazyModules(modules, dispatcher = Dispatcher.IO)`
info
Default dispatcher for coroutines engine is `Dispatchers.Default`