Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Koin context has not been initialized in rememberKoinApplication #2127

Open
UKMIITB opened this issue Feb 5, 2025 · 7 comments
Open

Koin context has not been initialized in rememberKoinApplication #2127

UKMIITB opened this issue Feb 5, 2025 · 7 comments
Labels
question Usage question

Comments

@UKMIITB
Copy link

UKMIITB commented Feb 5, 2025

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. In a composable, added koinApplication & then this composable as SDK is called in client app in many places
  2. When calling this app is crashing in Android.

Expected behavior
App should not crash

Koin module and version:
koin-annotations-bom = "1.4.0"
koin-bom = "4.0.2"

Snippet or Sample project to help reproduce
Add a snippet or even a small sample project to help reproduce your case.

KoinApplication( 
    application = {  
        modules(adverseModule)
        androidContext(activity.applicationContext)  
    }) {  
           // compose related code here with VM etc.  
  }
@UKMIITB
Copy link
Author

UKMIITB commented Feb 6, 2025

@arnaudgiuliani
Can you please help with this

@arnaudgiuliani arnaudgiuliani added the question Usage question label Feb 6, 2025
@arnaudgiuliani
Copy link
Member

it won't start if you don't run global context. Do you run an SDK here?

@UKMIITB
Copy link
Author

UKMIITB commented Feb 6, 2025

@arnaudgiuliani
Yes I have a composable function which is part of SDK
And from the client app, this SDK dependency is added & the composable function is called.

Within the composable function, internally I want to use DI with koin as it has its own VM, repository etc.

@arnaudgiuliani
Copy link
Member

you need to use KoinIsolatedContext instead of KoinApplication then

@UKMIITB
Copy link
Author

UKMIITB commented Feb 6, 2025

Understood
Sorry misunderstood the documentation on this

One last question
In this case how to set androidContext ?

@UKMIITB
Copy link
Author

UKMIITB commented Feb 6, 2025

@arnaudgiuliani
Is this approach correct ?

object MyIsolatedKoinContext {
    private var _koinApp: KoinApplication? = null

    fun getOrCreateKoinApp(context: Context): KoinApplication {
        return _koinApp ?: koinApplication {
            androidContext(context.applicationContext)
            modules(myModule)
        }.also { _koinApp = it }
    }
}

And then inside the composable function

val koinApp = remember(Unit) {
        MyIsolatedKoinContext.getOrCreateKoinApp(activity.applicationContext)
    }

    KoinIsolatedContext(context = koinApp) {
 // composable function here
}

@arnaudgiuliani
Copy link
Member

You need to assemble that with the KoinIsolatedContext composable function: https://insert-koin.io/docs/reference/koin-compose/isolated-context#setup-isolated-context-with-compose

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Usage question
Projects
None yet
Development

No branches or pull requests

2 participants