Skip to content

Commit

Permalink
Fix KoinApplication to create the right Global context
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Oct 25, 2024
1 parent 52cd502 commit bd99e27
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.koin.compose.application

import androidx.compose.runtime.RememberObserver
import org.koin.core.Koin
import org.koin.core.KoinApplication
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.context.startKoin
Expand All @@ -12,6 +13,12 @@ class CompositionKoinApplicationLoader(
val koinApplication: KoinApplication,
) : RememberObserver {

var koin : Koin? = null

init {
start()
}

override fun onAbandoned() {
stop()
}
Expand All @@ -25,8 +32,10 @@ class CompositionKoinApplicationLoader(
}

private fun start() {
val koin = startKoin(koinApplication).koin
koin.logger.debug("$this -> started Koin Application $koinApplication")
if (KoinPlatform.getKoinOrNull() == null){
koin = startKoin(koinApplication).koin
koin!!.logger.debug("$this -> started Koin Application $koinApplication")
}
}

private fun stop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ inline fun rememberKoinApplication(koinApplication: KoinApplication): Koin {
val wrapper = remember {
CompositionKoinApplicationLoader(koinApplication)
}
return wrapper.koinApplication.koin
return wrapper.koin ?: error("Koin context has not been initialized in rememberKoinApplication")
}

0 comments on commit bd99e27

Please sign in to comment.