Thread
#multiplatform
    Charles Prado

    Charles Prado

    7 months ago
    Hi! I’m getting an
    InstanceCreateException
    on my iOS app when I try to do a request to an API from the shared module.
    Uncaught Kotlin exception: org.koin.core.error.InstanceCreationException: Could not create instance for [Singleton:'com.test.remote.BookingApi']
    Any idea why this is not being recognized on the iOS-side? (My
    Koin.kt
    and
    BookingAPI.kt
    files are described in the thread),
    My Koin.kt:
    fun initKoin(enableNetworkLogs: Boolean = true, appDeclaration: KoinAppDeclaration = {}) =
        startKoin {
            appDeclaration()
            modules(commonModule(enableNetworkLogs = enableNetworkLogs))
        }
    
    // called by iOS etc
    fun initKoin() = initKoin(enableNetworkLogs = true) {}
    
    fun commonModule(enableNetworkLogs: Boolean) = module {
        single { createJson() }
        single { createHttpClient(get(), enableNetworkLogs = enableNetworkLogs, get()) }
        single { BookingApi(get()) }
    }
    
    fun createJson() = Json { isLenient = true; ignoreUnknownKeys = true }
    
    fun createHttpClient(json: Json, enableNetworkLogs: Boolean, projUserAgent: ProjUserAgent): HttpClient {
        return HttpClient {
            install(JsonFeature) {
                serializer = KotlinxSerializer(json)
            }
            // Full configuration.
            install(UserAgent) {
                agent = projUserAgent.platform
            }
    
            if (enableNetworkLogs) {
                install(Logging) {
                    logger = Logger.SIMPLE
                    level = LogLevel.ALL
                }
            }
        }
    }
    The API file:
    package com.test.remote
    
    import com.test.model.*
    import com.test.model.requestModels.BookingRequest
    import io.ktor.client.HttpClient
    import io.ktor.client.request.*
    import io.ktor.client.statement.*
    import io.ktor.http.*
    import org.koin.core.component.KoinComponent
    
    private const val BASE_URL = "<https://MY_DOMAIN.net/api/v1/booking>"
    
    class BookingApi(private val client: HttpClient) : KoinComponent {
    
        suspend fun getAvailableDates(officeId: Int): WeekAvailabilityList =
            client.get("$BASE_URL/dates") {
                parameter("officeId", officeId)
            }
    }
    g

    Guilherme Delgado

    7 months ago
    Fixed The problem was related to missing DI declarations that would throw runtime exceptions