Can i create 2 datastore in kmp? i create DataStor...
# multiplatform
a
Can i create 2 datastore in kmp? i create DataStore<Profile> and all works, but when i create DataStore<Settings> my app crash with this error: java.lang.ClassCastException: ProfileDto cannot be cast to SettingsDto
m
Datastore name are different
a
Copy code
actual val dataStoreModule: Module = module {
    single {
        createProfileDataStore {
            createAndroidFilePath(get(), "profile.json")
        }
    }
    single {
        createSettingsDataStore {
            createAndroidFilePath(get(), "settings.json")
        }
    }
}
Copy code
private fun createAndroidFilePath(context: Context, relative: String): String =
    context.filesDir.resolve(relative).absolutePath
m
Without di try it work or not. Manually object instance
a
I solve problem, i dont know good this method or not, but it's works
Copy code
enum class DataStoreType {
    Profile,
    Settings
}

actual val datastoreModule: Module = module {
    single<DataStore<ProfileDto>>(
        named(DataStoreTypes.Profile)
    ) {
        createProfileDataStore {
            createAndroidFilePath(get(), "profile.json")
        }
    }
    single<DataStore<SettingsDto>>(
        named(DataStoreTypes.Settings)
    ) {
        createTokensDataStore {
            createAndroidFilePath(get(), "settings.json")
        }
    }
}
m
Ok ok type specific instance name it great work. Previously code koin not know same datatype instance so issue occurred
a
Yep, but Hilt in native android work without this ;D
With 2 datastore<>
m
Ask #C67HDJZ2N channel may be improved code it
a
Thanks