Farhazul Mullick
01/15/2024, 10:40 AMactual inline fun <reified T : ViewModel, reified S> Module.viewModelScopedDefinition(
qualifier: Qualifier?,
noinline definition: Definition<T>,
){
scope<S> {
scoped(qualifier = qualifier, definition = definition)
}
}
module {
viewModelScopedDefinition<ParentAuthViewModel, ParentAuthScope> {
ParentAuthViewModel(
parentGetOtpUseCase = get(),
......
)
}
}
class ParentAuthScope private constructor(): KoinScopeComponent{
override val scope: Scope by getOrCreateScope()
companion object: BaseScope<ParentAuthScope>() {
/**
* Create a new ParentAuthScope instance.
*/
override fun newInstance(): ParentAuthScope = run {
val instance = ParentAuthScope()
INSTANCE = instance
instance
}
}
fun close() {
if (scope.isNotClosed())
scope.close()
}
}
abstract class BaseScope<T: KoinScopeComponent> {
protected var INSTANCE: T? = null
fun instantiate(): T {
return if (INSTANCE != null) {
/**
* This scope reference is a container in which koin puts all the objects.
* This objects can be accessed via ex: [childAuthScope.scope.get<ChildAuthScope>()].
* If this container is closed i.e scope.close() is called and we are still trying to access
* objects using this scope then koin throws [ClosedScopeException].
* So we are creating a new reference of this class which will eventually create new scope-container.
* **/
if (INSTANCE?.scope?.closed == true) {
newInstance()
} else {
INSTANCE!!
}
}
else {
/** Create a new scope **/
newInstance()
}
}
protected abstract fun newInstance(): T
}
val viewModel = _remember_ *{* ParentAuthScope.instantiate().scope.get<ParentAuthViewModel>() *}*
This is throwing an exception when i run iOS build can anyone help? This what my stack-trace looks like
org.koin.core.error.InstanceCreationException
Could not create instance for '[Scoped:'packageName.ParentAuthViewModel',scope:q:'packageName.commonauth.di.ParentAuthScope']' Uncaught Kotlin exception: org.koin.core.error.InstanceCreationException: Could not create instance for '[Scoped:'packageName.viewmodel.ParentAuthViewModel',scope:q:'packageName.ParentAuthScope']'
at 0 shared 0x1092be273 kfun:kotlin.Exception#<init>(kotlin.String?;kotlin.Throwable?){} + 143
at 1 shared 0x109d6e523 kfun:org.koin.core.error.InstanceCreationException#<init>(kotlin.String;kotlin.Exception){} + 143
at 2 shared 0x109d6fd47 kfun:org.koin.core.instance.InstanceFactory#create(org.koin.core.instance.InstanceContext){}1:0 + 1623
at 3 shared 0x109d70a9b kfun:org.koin.core.instance.ScopedInstanceFactory#create(org.koin.core.instance.InstanceContext){}1:0 + 495
at 4 shared 0x109d7157b kfun:org.koin.core.instance.ScopedInstanceFactory.get$lambda$0#internal + 375
at 5 shared 0x109d7163f kfun:org.koin.core.instance.ScopedInstanceFactory.$get$lambda$0$FUNCTION_REFERENCE$0.invoke#internal + 95
at 6 shared 0x109d71763 kfun:org.koin.core.instance.ScopedInstanceFactory.$get$lambda$0$FUNCTION_REFERENCE$0.$<bridge-UNN>invoke(){}#internal + 91
at 7 shared 0x10a34c08f kfun:kotlin.Function0#invoke(){}1:0-trampoline + 99
at 8 shared 0x109d85233 kfun:org.koin.mp.KoinPlatformTools#synchronized(org.koin.mp.Lockable;kotlin.Function0<0:0>){0§<kotlin.Any?>}0:0 + 391
at 9 shared 0x109d71123 kfun:org.koin.core.instance.ScopedInstanceFactory#get(org.koin.core.instance.InstanceContext){}1:0 + 1095
at 10 shared 0x10a3ed51b kfun:org.koin.core.instance.InstanceFactory#get(org.koin.core.instance.InstanceContext){}1:0-trampoline + 67
at 11 shared 0x109d793f3 kfun:org.koin.core.registry.InstanceRegistry#resolveInstance(org.koin.core.qualifier.Qualifier?;kotlin.reflect.KClass<*>;org.koin.core.qualifier.Qualifier;org.koin.core.instance.InstanceContext){0§<kotlin.Any?>}0:0? + 363
at 12 shared 0x109d7f2ff kfun:org.koin.core.scope.Scope.resolveValue#internal + 855
at 13 shared 0x109d7eec7 kfun:org.koin.core.scope.Scope.resolveInstance#internal + 1519