-
Notifications
You must be signed in to change notification settings - Fork 71
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
.remove() in Seriliazation module #81
Comments
This is currently the expected behavior, because In the meantime, if you use |
Hi. thanks for the quick response I can still pass |
Encountered this problem too. It would be great if it could be fixed. And workaround with encodeValue(null) not works for me Setttings.encodeValue(
serializer = serializer.nullable,
key = key.key,
value = null
) |
Can you say more about what doesn't work? |
Hello, The model: @kotlinx.serialization.Serializable
data class User(
val name: String,
val company: String? = null
) The repository: class UserRepository : KoinComponent {
private val settings: Settings by inject()
fun addUser(user: User) {
settings.encodeValue(User.serializer(), user.name, user)
}
fun deleteUser(user: User) {
settings.encodeValue(User.serializer().nullable, user.name, null)
}
fun getKeysSize() = settings.keys.size
} The test: @Test
fun addAndRemoveUser() {
// given
val repo = AccountSettingsRepository()
val user = User("Bob")
// when
repo.addUser(user)
repo.deleteUser(user)
// then
assertEquals(0, repo.getKeysSize())
} The test fails because after the
I guess the error is that it adds nullability on the User object instead of removing the constructed keys upon the User. Maybe it is because I use a key that is built upon an element of User (and my real case uses a key that is the concatenation of three elements). For now, my hack to circumvent this issue is to do a brutal: settings.keys
.filter { it.startsWith(user.name) }
.forEach { settings.remove(it) } Hope this will help to find a fix for this bug. Maybe an explicit method like |
Thanks, that makes it a bit more clear. I do like the idea of a serialization-aware versions of |
Here's my current thinking about handling these APIs when a value is partially present. (Ironically, the complications are in the opposite place that I expected). In the existing API, But there is a choice to make in how to handle There's a draft PR in #117 which uses option 1 for the |
Hi, I encountered the same problem. I have this code in my application: class DecodedItem<T> internal constructor(
private val key: String,
private val settings: ObservableSettings,
private val serializer: KSerializer<T>
) {
@OptIn(ExperimentalSerializationApi::class)
var value: T?
get() {
return settings.decodeValueOrNull(key = key, serializer = serializer)
}
set(newValue) {
_mutableStateFlow.value = newValue
if (newValue == null) {
settings.remove(key)
return
}
settings.encodeValue(serializer, key, newValue)
}
private val _mutableStateFlow: MutableStateFlow<T?> = MutableStateFlow(value)
val stateFlow: StateFlow<T?> = _mutableStateFlow.asStateFlow()
} After applying Regarding your last comment, I think it would be logical to use first option to handle So far I got around this problem by creating another logical variable responsible for data availability. |
This will be released soon as part of version 1.1. I opted to include a flag in |
This was included in version 1.1.0 |
I am trying to remove() a serialized class but
settings.remove("key")
doesn't seems to work for Serialized module.I am using
no-arg
module to instantiatesettings
variable. Mysettings.remove(key)
works with basic datatypes likeint
string
but it doesn't work with theserialized
classes.For eg :- If I have a class such as
data class User(name: String, number: String)
I store it usingsettings.encodeValue(User.serializer(), "key", user)
now it does save it correctly no issues till here. Now If at some point I want to remove myUser
from the shared preference I try to do it withsettings.remove("key")
and it doesn't do anything I can still get myUser
object from thepreferences
like thissettings.decodeValueOrNull(User.serializer(), "key")
where as expected should be null.Also if i try to check if value is stored for my particular key using
settings.contains("key")
then it always return false no matter if I have that in mypreference
or not.Same happens with iOS as well.
Also the only thing which works is either I clear all of my preference using
settings.clear()
or I explicitly set myUser
variable asnull
as a workaround. Is this the expected behaviour or am I missing something withSerialization module
?The text was updated successfully, but these errors were encountered: