如何解决 Kotlin Multiplaform 中的“org.koin.core.error.InstanceCreationException:无法为 '[Singleton:']' 创建实例”

问题描述 投票:0回答:1

我正在使用 Kotlin Multiplatform 开发 SDK

我们想要开发的功能是检查互联网连接并编写一些单元测试用例来实现我们正在使用的 MVVM 架构

expect class Connectivity {
    fun isInternetConnected(): Boolean
}

对应的实际类是

actual class Connectivity(val context: Context) {
    actual fun isInternetConnected(): Boolean {
        val connectivityManager =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val capabilities =
            connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)

        if (capabilities?.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) == true) {
            PlatformLogger("Internet").logI("NetworkCapabilities.TRANSPORT_CELLULAR", null)
            return true
        } else if (capabilities?.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) == true) {
            PlatformLogger("Internet").logI("NetworkCapabilities.TRANSPORT_WIFI", null)
            return true
        } else if (capabilities?.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) == true) {
            PlatformLogger("Internet").logI("NetworkCapabilities.TRANSPORT_ETHERNET", null)
            return true
        }
        PlatformLogger("Internet").logI("No Internet available", null)

        return false
    }
}

并且:

actual class Connectivity {
    @OptIn(ExperimentalForeignApi::class)
    actual fun isInternetConnected(): Boolean {
        memScoped {
            val reachability = SCNetworkReachabilityCreateWithName(null, "www.google.com")
            val flags = alloc<UIntVar>()
            val gotFlags = SCNetworkReachabilityGetFlags(reachability, flags.ptr)
            if (!gotFlags) {
                PlatformLogger("Network-IOS").logI("Could not fetch the network reachability flags")
                return false
            }
            return (flags.value and kSCNetworkReachabilityFlagsReachable) != 0u
        }
    }
}

在创建存储库时,我们需要连接对象的实例,我们使用 Koin 注入依赖项:

single<TrackerRepository> {
    TrackerRepository(
        local = get<TrackerLocalDataSource>(),
        network = get<TrackerNetworkDataSource>(),
        connectivity = get<Connectivity>(),
    )
}

我收到此错误:

org.koin.core.error.InstanceCreationException:无法为'[Singleton:'com.autodesk.adp_mobile_sdk.tracker.data.repository.TrackerRepository']'创建实例

在modules.android中我添加了

 single<Connectivity>{Connectivity(get())}
kotlin kotlin-multiplatform koin
1个回答
0
投票

确保您是按顺序定义的。 例如,如果您的模块中有类似的内容:

single<TrackerRepository> {
    TrackerRepository(
        local = get<TrackerLocalDataSource>(),
        network = get<TrackerNetworkDataSource>(),
        connectivity = get<Connectivity>(),
    )
}
 single<Connectivity>{Connectivity(get())}

这不起作用,因为 Koin 将从定义 TrackerRepository 开始,它将去搜索应该定义的连接。它找不到它就会抛出错误。

所以请确保您按照这样的顺序进行定义

 single<Connectivity>{Connectivity(get())}

 single<TrackerRepository> {
        TrackerRepository(
            local = get<TrackerLocalDataSource>(),
            network = get<TrackerNetworkDataSource>(),
            connectivity = get<Connectivity>(),
        )
    }
© www.soinside.com 2019 - 2024. All rights reserved.