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

Compose Multiplatform | iosMain | InstanceCreationException: Could not create instance #2107

Open
javierpe opened this issue Jan 5, 2025 · 0 comments
Labels
question Usage question

Comments

@javierpe
Copy link

javierpe commented Jan 5, 2025

Describe the bug
Hi I have this error in my Compose Multiplatform Project with iOS:
InstanceCreationException: Could not create instance for '[Factory: 'com.uniciti.sos.storage.api.SaveUserLocationUseCase']'

commonMain:

expect class PlatformLocationService {
    suspend fun start(locationServiceConfig: LocationServiceConfig = LocationServiceConfig())
    suspend fun stop()
}

androidMain:

actual class PlatformLocationService(
    private val context: Context
) {

    actual suspend fun start(
        locationServiceConfig: LocationServiceConfig
    ) {  ...  }

    actual suspend fun stop() { ... }
}

iosMain:

actual class PlatformLocationService : KoinComponent {

    private val saveUserLocationUseCase: SaveUserLocationUseCase by inject()

    actual suspend fun start(
        locationServiceConfig: LocationServiceConfig
    ) {...}

    actual suspend fun stop() {... }
}

I have a shared module beacuse I need background service location in both platforms:

commonMain:
expect fun getSharedLocationModule(): Module

iosMain:

actual fun getSharedLocationModule(): Module = module {
    factory { PlatformLocationService() }
    factory { PlatformNotificationManager() }
}

I have a single module in commonMain of composeApp:

fun appModules() = listOf(
    getSharedLocationModule()
)

And finally I have my composable ui:

@Composable
fun App() {
    MaterialTheme {
        KoinApplication(
            application = {
                defaultModule()
                modules(appModules())
            }
        ) { ... }
    }
}

This only happens when I have dependencies injected into the impl classes:

class SaveUserLocationUseCaseImpl (
    private val userLocationRepository: UserLocationRepository
): SaveUserLocationUseCase {

    override suspend fun invoke(locationData: LocationData) {
        userLocationRepository.save(locationData)
    }
}

Please help me, I new in KMP 😆

@arnaudgiuliani arnaudgiuliani added the question Usage question label Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Usage question
Projects
None yet
Development

No branches or pull requests

2 participants