russhwolf / multiplatform-settings

A Kotlin Multiplatform library for saving simple key-value data
Apache License 2.0
1.49k stars 64 forks source link

clear function doesn't work #182

Closed vitaly72 closed 5 months ago

vitaly72 commented 5 months ago

I use multiplatform-settings-no-arg in KMP project and I need to clear the values in the settings, none of these methods work

fun clear() {
  settings.remove("name")
  settings -= "name"
  settings["name"] = null
}
russhwolf commented 5 months ago

Hard to say much without seeing more code. Do you have a sample project? What platforms are you running on?

vitaly72 commented 5 months ago

Project running on Android and iOS. it's DI Koin:

private val memoryModule = module {
    singleOf(::Settings)
}

it's repository implementation

internal class NameLocalDataSource(
    private val settings: Settings,
) {

    fun save(name: Name) {
        settings.encodeValue(
            serializer = Name.serializer(),
            key = "name",
            value = name,
        )
    }

    fun get(): Name = settings.decodeValue(
        serializer = Name.serializer(),
        key = "name",
        defaultValue = emptyName(),
    )

    fun clear() {
        settings.remove("name")
        settings -= "name"
        settings["name"] = null
    }

    private fun emptyName(): Name = Name(
        first = "",
        last = "",
    )
}
russhwolf commented 5 months ago

If you're using serialization stuff you'll want removeValue() instead of remove().