koin
koin copied to clipboard
KClass for Objective-C classes is not supported yet
Describe the bug Creating this bug since this one is actually not resolved: https://github.com/InsertKoinIO/koin/issues/1016
So the issue is currently we are not able to inject Objective-C classes directly that's why I use a wrapper class to hold Objective-C class inside. (In the example it is UserDefaults
)
If I try below app runs with no problem
data class NativeDependencyWrapper(val userDefaults: NSUserDefaults)
internal actual val settingsModule = module {
single<Settings> {
AppleSettings(get<NativeDependencyWrapper>().userDefaults)
}
}
internal fun getIOSModule(userDefaults: NSUserDefaults) = module {
single { NativeDependencyWrapper(userDefaults) }
}
But expected behaviour should be like below, we shouldn't need this extra wrapper:
internal actual val settingsModule = module {
single<Settings> { AppleSettings(get()) }
}
internal fun getIOSModule(userDefaults: NSUserDefaults) = module {
single { userDefaults }
}
Here is a PR that I remove this wrapper class: https://github.com/Oztechan/CCC/pull/1525/files so it can be used as reproducer
To Reproduce Steps to reproduce the behavior: Run the ios app in mentioned PR branch, after the app starts running it will crash with this log
Function doesn't have or inherit @Throws annotation and thus exception isn't propagated from Kotlin to Objective-C/Swift as NSError.
It is considered unexpected and unhandled instead. Program will be terminated.
Uncaught Kotlin exception: kotlin.IllegalStateException: KClass for Objective-C classes is not supported yet
at 0 CCC 0x10ea5304b kfun:kotlin.Throwable#<init>(kotlin.String?){} + 107
at 1 CCC 0x10ea4bf57 kfun:kotlin.Exception#<init>(kotlin.String?){} + 103
at 2 CCC 0x10ea4c197 kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 103
at 3 CCC 0x10ea4c857 kfun:kotlin.IllegalStateException#<init>(kotlin.String?){} + 103
at 4 CCC 0x10ea74a3c kfun:kotlin.native.internal.KClassUnsupportedImpl#hashCode(){}kotlin.Int + 316
at 5 CCC 0x10eab57c9 kfun:kotlin.collections.HashMap.hash#internal + 217
at 6 CCC 0x10eab6445 kfun:kotlin.collections.HashMap.findKey#internal + 357
at 7 CCC 0x10eab35c9 kfun:kotlin.collections.HashMap#get(1:0){}1:1? + 217
at 8 CCC 0x10ef2686a kfun:org.koin.ext#getFullName__at__kotlin.reflect.KClass<*>(){}kotlin.String + 314
at 9 CCC 0x10ef09548 kfun:org.koin.core.definition#indexKey(kotlin.reflect.KClass<*>;org.koin.core.qualifier.Qualifier?;org.koin.core.qualifier.Qualifier){}kotlin.String + 520
at 10 CCC 0x10ef11278 kfun:org.koin.core.module.Module#indexPrimaryType(org.koin.core.instance.InstanceFactory<*>){} + 488
at 11 CCC 0x10f119a01 kfun:com.oztechan.ccc.provider.di.module.getIOSModule$lambda$1#internal + 929
at 12 CCC 0x10f119b7b kfun:com.oztechan.ccc.provider.di.module.$getIOSModule$lambda$1$FUNCTION_REFERENCE$2.invoke#internal + 107
at 13 CCC 0x10f119c7b kfun:com.oztechan.ccc.provider.di.module.$getIOSModule$lambda$1$FUNCTION_REFERENCE$2.$<bridge-UNNN>invoke(-1:0){}#internal + 107
at 14 CCC 0x10ef2621d kfun:org.koin.dsl#module(kotlin.Boolean;kotlin.Function1<org.koin.core.module.Module,kotlin.Unit>){}org.koin.core.module.Module + 365
at 15 CCC 0x10ef2635b kfun:org.koin.dsl#module$default(kotlin.Boolean;kotlin.Function1<org.koin.core.module.Module,kotlin.Unit>;kotlin.Int){}org.koin.core.module.Module + 155
at 16 CCC 0x10f11959d kfun:com.oztechan.ccc.provider.di.module#getIOSModule(platform.Foundation.NSUserDefaults){}org.koin.core.module.Module + 205
at 17 CCC 0x10f118c4b kfun:com.oztechan.ccc.provider.di.initKoin$lambda$0#internal + 475
at 18 CCC 0x10f119282 kfun:com.oztechan.ccc.provider.di.$initKoin$lambda$0$FUNCTION_REFERENCE$0.invoke#internal + 114
at 19 CCC 0x10f11939b kfun:com.oztechan.ccc.provider.di.$initKoin$lambda$0$FUNCTION_REFERENCE$0.$<bridge-UNNN>invoke(-1:0){}#internal + 107
at 20 CCC 0x10ef283a0 kfun:org.koin.core.context.MutableGlobalContext#startKoin(kotlin.Function1<org.koin.core.KoinApplication,kotlin.Unit>){}org.koin.core.KoinApplication + 688
at 21 CCC 0x10ef06ee8 kfun:org.koin.core.context#startKoin(kotlin.Function1<org.koin.core.KoinApplication,kotlin.Unit>){}org.koin.core.KoinApplication + 280
at 22 CCC 0x10f118375 kfun:com.oztechan.ccc.provider.di#initKoin(platform.Foundation.NSUserDefaults;com.oztechan.ccc.analytics.AnalyticsManager){}org.koin.core.KoinApplication + 437
at 23 CCC 0x10f1f32d0 objc2kotlin.3907 + 240
at 24 CCC 0x10e86294e $s3CCC4koinSo017ProviderKoin_coreD0CvpfiADyXEfU_ + 254
at 25 CCC 0x10e862838 $s3CCC4koin_WZ + 8
at 26 libdispatch.dylib 0x110d0a74d _dispatch_client_callout + 7
at 27 libdispatch.dylib 0x110d0bc9a _dispatch_once_callout + 65
at 28 libswiftCore.dylib 0x7fff2f3a0599 swift_once + 25
at 29 CCC 0x10e862a30 $s3CCC4koinSo017ProviderKoin_coreD0Cvau + 48
at 30 CCC 0x10e885ff3 $s3CCC11ApplicationVACycfC + 1203
at 31 CCC 0x10e8887c8 $s3CCC11ApplicationV7SwiftUI3AppAadEPxycfCTW + 8
at 32 SwiftUI 0x7fff562e5375 $s7SwiftUI3AppPAAE4mainyyFZ + 46
at 33 CCC 0x10e888752 $s3CCC11ApplicationV5$mainyyFZ + 34
at 34 CCC 0x10e8887d8 main + 8
at 35 ??? 0x11d90330f 0x0 + 4790956815
Expected behavior App should run
Koin project used and used version (please complete the following information):
koin-core version 3.3.0, 3.3.2
did you tried latest Koin in 3.3.2?
@arnaudgiuliani just tried and yes it still give the same error
I confirm what @mustafaozhan is reporting. My project is using Koin core 3.3.2
I need to continue investigating. The more I an gather sample, the more I can test this 🤔
Seems a limitation of Kotlin/native with reified type as I. can see.
@arnaudgiuliani Yeap sure take your time 🙂 as for the sample you can try running my app in this branch https://github.com/Oztechan/CCC/tree/1524-Remove_NativeDependencyWrapper
The app has submodules, so you need to clone them too and the crash is runtime, so you need to run the iOS app in order to get the crash. README.md has both How to clone
and How to run > iOS
sections, located here: https://github.com/Oztechan/CCC#readme
Great, thanks for your help.
Rescheduling it for new core release 👍
Still stuck due to Kotlin native limitations. Operations are not allowed on KClass/ObjC: "KClass for Objective-C classes is not supported yet"
I've tried to hack around definition ObjCObject type also, nothing great. Will ask Jetbrains about some clues on it.
Looking for a solution around. Let's keep an eye for Koin 3.5 on it
still waiting for some updates
I have the same problem with Koin 3.5.3, as a workaround for a singleton I used an object
as dependency provider for my Objective-C wrapper