I am developing a library (an SDK) and i use koin ...
# koin
z
I am developing a library (an SDK) and i use koin internally, i want my SDK to have a
connect
and
disconnect
functions, which will internally call
startKoin
and
stopKoin
Will this mean that an app consuming my library and also uses koin be affected? how can i avoid this? The reason i want to call
stopKoin
is to clear all the singeltons my SDK creates as it initializes. What would be the best practice on using koin in a library project?
a
This could be an usecase for scopes, have a custom scope for your library.
z
I actually found the answer in the docs eventually https://insert-koin.io/docs/reference/koin-core/context-isolation
👍 1
a
start/stop Koin is a good practice for your SDK. Scope can be a solution too, but more complex.
z
If i use stopKoin, wouldnt that affect an app consuming my SDK if they use koin as well?, also if i call startKoin in my SDK i could crash an app that has called startKoin itself
The solution i found with context isolation works well, but i have a problem clearing my classes, i use stopKoin or close, i cant i use my Koin instance again https://kotlinlang.slack.com/archives/C67HDJZ2N/p1720540650621969
a
If i use stopKoin, wouldnt that affect an app consuming my SDK if they use koin as well?, also if i call startKoin in my SDK i could crash an app that has called startKoin itself
if Isolated, no impact
You need to setup your SDK with Koin Isolated Context
z
I am setting it up with isolated context, but if i understand correctly from the docs, i should use close, rather than stopKoin, i can call close on my koin context, but stopKoin will close the GlobalContext
a
yes it's stopKoin from current instance. Then yes close