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

KClass for Objective-C classes is not supported yet #1016

Closed
mustafaozhan opened this issue Feb 17, 2021 · 18 comments
Closed

KClass for Objective-C classes is not supported yet #1016

mustafaozhan opened this issue Feb 17, 2021 · 18 comments
Labels
multiplatform kmp status:checking currently in analysis - discussion or need more detailed specs type:issue
Milestone

Comments

@mustafaozhan
Copy link
Contributor

mustafaozhan commented Feb 17, 2021

Describe the bug
The bug appears when you want to get a Objective-C class

Koin version 3.0.1-alpha-3

Edit: 3.1.0 remains the same
Edit: 3.1.2 remains the same

a simple example to demonstrate

// works
fun initIOS(userDefaults: NSUserDefaults) = initClient(
    module {
       single<Settings> { AppleSettings(userDefaults) }
    }
)

//fails
fun initIOS(userDefaults: NSUserDefaults) = initClient(
    module {
       single<NSUserDefaults> { userDefaults }
       single<Settings> { AppleSettings(get()) }
    }
)

It compiles fine but getting run time exception on IOS, the log below

Debug: (Kermit) starting koin
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                                 0x000000010b61082f kfun:kotlin.Throwable#<init>(kotlin.String?){} + 95
    at 1   CCC                                 0x000000010b608f8d kfun:kotlin.Exception#<init>(kotlin.String?){} + 93
    at 2   CCC                                 0x000000010b6091fd kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 93
    at 3   CCC                                 0x000000010b6098ad kfun:kotlin.IllegalStateException#<init>(kotlin.String?){} + 93
    at 4   CCC                                 0x000000010b6513cf kfun:kotlin.native.internal.KClassUnsupportedImpl#hashCode(){}kotlin.Int + 335
    at 5   CCC                                 0x000000010b8de31f kfun:org.koin.core.definition.BeanDefinition#hashCode(){}kotlin.Int + 383
    at 6   CCC                                 0x000000010b626072 kfun:kotlin.collections.HashMap.hash#internal + 226
    at 7   CCC                                 0x000000010b627736 kfun:kotlin.collections.HashMap#addKey(1:0){}kotlin.Int + 486
    at 8   CCC                                 0x000000010b6310ff kfun:kotlin.collections.HashSet#add(1:0){}kotlin.Boolean + 207
    at 9   CCC                                 0x000000010b8e77a7 kfun:org.koin.core.module#addDefinition@kotlin.collections.HashSet<org.koin.core.definition.BeanDefinition<*>>(org.koin.core.definition.BeanDefinition<*>){} + 439
    at 10  CCC                                 0x000000010b472354 kfun:com.github.mustafaozhan.ccc.client.di.initIOS$lambda-1#internal + 1252
    at 11  CCC                                 0x000000010b472541 kfun:com.github.mustafaozhan.ccc.client.di.$initIOS$lambda-1$FUNCTION_REFERENCE$52.invoke#internal + 97
    at 12  CCC                                 0x000000010b4725c1 kfun:com.github.mustafaozhan.ccc.client.di.$initIOS$lambda-1$FUNCTION_REFERENCE$52.$<bridge-UNNN>invoke(-1:0){}#internal + 97
    at 13  CCC                                 0x000000010b8fefed kfun:org.koin.dsl#module(kotlin.Boolean;kotlin.Boolean;kotlin.Function1<org.koin.core.module.Module,kotlin.Unit>){}org.koin.core.module.Module + 381
    at 14  CCC                                 0x000000010b8ff184 kfun:org.koin.dsl#module$default(kotlin.Boolean;kotlin.Boolean;kotlin.Function1<org.koin.core.module.Module,kotlin.Unit>;kotlin.Int){}org.koin.core.module.Module + 276
    at 15  CCC                                 0x000000010b471a82 kfun:com.github.mustafaozhan.ccc.client.di#initIOS(platform.Foundation.NSUserDefaults){}org.koin.core.KoinApplication + 354
    at 16  CCC                                 0x000000010b5aaec8 objc2kotlin.3553 + 216
    at 17  CCC                                 0x000000010b2a43d2 $s3CCC6CCCAppVACycfC + 18
    at 18  CCC                                 0x000000010b2a4bc9 $s3CCC6CCCAppV7SwiftUI3AppAadEPxycfCTW + 9
    at 19  SwiftUI                             0x000000010eebc65f $s7SwiftUI3AppPAAE4mainyyFZ + 47
    at 20  CCC                                 0x000000010b2a4b61 $s3CCC6CCCAppV5$mainyyFZ + 33
    at 21  CCC                                 0x000000010b2a4be4 main + 20
    at 22  libdyld.dylib                       0x0000000112b6895d start + 1
    at 23  ???                                 0x0000000000000001 0x0 + 1
CoreSimulator 725.10 - Device: iPhone 11 Pro Max (1C7C1A0E-2FE5-47FF-AAB1-57530F0DAD34) - Runtime: iOS 14.0 (18A5319g) - DeviceType: iPhone 11 Pro Max
@Baileypollard
Copy link

Do we know the cause of this yet?

@mustafaozhan
Copy link
Contributor Author

@Baileypollard yes, from the log I understand that koin doesn't support it currently. KClass for Objective-C classes is not supported yet

@JohNan
Copy link

JohNan commented Aug 20, 2021

I have run into the same problem. My workaround for now is to create a wrapper class in kotlin.

fun iosModule(application: UIApplication) = module {
    single { UIApplicationWrapper(application) }
}

data class UIApplicationWrapper(val uiApplication: UIApplication)

Seems to work :)

@slipdef
Copy link

slipdef commented Sep 2, 2021

Any progress on that? It blocks us to use Koin to resolve iOS dependencies.

@osrl
Copy link

osrl commented Oct 18, 2021

Is this related to what I've encountered?

I can't get from ios if the dependency is defined with interface:

single<TokenSource> {
        TokenSourceImpl()
} //this doesn't work


single {
        TokenSourceImpl()
} //this works

//kotlin function to get
fun Koin.get(objCClass: ObjCClass, parameter: Any): Any {
    val kClazz = getOriginalKotlinClass(objCClass)!!
    return get(kClazz) { parametersOf(parameter) }
}
//swift usage
lazy var tokenSource = koin.get(objCClass: TokenSourceImpl.self) as! TokenSourceImpl

Error:
Uncaught Kotlin exception: org.koin.core.error.NoBeanDefFoundException: No definition found for class:'com.foo.main.ktor.TokenSourceImpl'. Check your definitions!

@zakrodionov
Copy link

Same issue in Kmm

@osrl
Copy link

osrl commented Oct 21, 2021

My issue was not related to this. I needed to use ObjCProtocol instead, if anyone is interested.

@mustafaozhan
Copy link
Contributor Author

@JohNan yes I confirm also,
It works as below

	data class NativeDependencyWrapper(val userDefaults: NSUserDefaults)
	single { NativeDependencyWrapper(userDefaults) }

and then I can use it

    AppleSettings(get<NativeDependencyWrapper>().userDefaults)

but we can not take this as solution it is just a workaround, I think koin should support this :)

@mykola-dev
Copy link

i would really appreciate if someone explain if this is a kmm limitation or the koin library issue? just curious why we can't use ios specific dependencies directly

@philhinco
Copy link

@osrl What do you mean with ObjCProtocol? I‘m trying to koin.get an interface that I defined in KMP and implemented in iOS, but it fails currently.

@osrl
Copy link

osrl commented Jan 18, 2022

objCProtocol instead of objCClass as parameter name

@philhinco
Copy link

@osrl Ah, I see. And what would be the way to call it from Swift? I‘m trying to create a function or property wrapper but can‘t get it to work. The plain call to my Kotlin extension function I wrote works without problem, but I would want to make an abstraction that does the whole dance with as casting etc.

@stale
Copy link

stale bot commented May 18, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the status:wontfix label May 18, 2022
@mustafaozhan
Copy link
Contributor Author

Still looking forward to seeing this issue fixed 🙂

@stale stale bot removed the status:wontfix label May 18, 2022
@arnaudgiuliani arnaudgiuliani added type:issue multiplatform kmp status:checking currently in analysis - discussion or need more detailed specs labels Jun 27, 2022
@arnaudgiuliani arnaudgiuliani added this to the 3.2.1 milestone Jun 27, 2022
@arnaudgiuliani arnaudgiuliani modified the milestones: 3.2.1, 3.3.0 Aug 29, 2022
@arnaudgiuliani
Copy link
Member

Reified type in Swift is making problems I guess. I've seen another issue related to this: #1316

arnaudgiuliani added a commit that referenced this issue Dec 14, 2022
@arnaudgiuliani
Copy link
Member

Let's see how it goes. Feel free to reopen if needed

@mustafaozhan
Copy link
Contributor Author

@arnaudgiuliani sure thanks a lot! Finger crossed for 3.3.0 🤞 🙂

@mustafaozhan
Copy link
Contributor Author

@arnaudgiuliani this doesn't work in new version, could you re-open it or should I fill a new one ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
multiplatform kmp status:checking currently in analysis - discussion or need more detailed specs type:issue
Projects
None yet
Development

No branches or pull requests

9 participants