MutablePreferences
class MutablePreferences : Preferences
kotlin.Any | ||
↳ | androidx.datastore.preferences.core.Preferences | |
↳ | androidx.datastore.preferences.core.MutablePreferences |
Mutable version of Preferences. Allows for creating Preferences with different key-value pairs.
Summary
Public methods | |
---|---|
Map<Preferences.Key<*>, Any> |
asMap() Retrieve a map of all key preference pairs. |
Unit |
clear() |
operator Boolean |
contains(key: Preferences.Key<T>) Returns true if this Preferences contains the specified key. |
Boolean | |
operator T? |
get(key: Preferences.Key<T>) Get a preference with a key. |
Int |
hashCode() |
operator Unit |
minusAssign(key: Preferences.Key<*>) Removes the preference with the given key from this MutablePreferences. |
operator Unit |
plusAssign(prefs: Preferences) Appends or replaces all pairs from prefs to this MutablePreferences. |
operator Unit |
plusAssign(pair: Preferences.Pair<*>) Appends or replaces all pair to this MutablePreferences. |
Unit |
putAll(vararg pairs: Preferences.Pair<*>) Appends or replaces all pairs to this MutablePreferences. |
T |
remove(key: Preferences.Key<T>) Remove a preferences from this MutablePreferences. |
operator Unit |
set(key: Preferences.Key<T>, value: T) Set a key value pair in MutablePreferences. |
String |
toString() For better debugging. |
Inherited functions | |
---|---|
Public methods
asMap
fun asMap(): Map<Preferences.Key<*>, Any>
Retrieve a map of all key preference pairs. The returned map is unmodifiable, and attempts to mutate it will throw runtime exceptions.
Return | |
---|---|
a map containing all the preferences in this Preferences |
clear
fun clear(): Unit
contains
operator fun <T> contains(key: Preferences.Key<T>): Boolean
Returns true if this Preferences contains the specified key.
Parameters | |
---|---|
key: Preferences.Key<T> | the key to check for |
get
operator fun <T> get(key: Preferences.Key<T>): T?
Get a preference with a key. If the key is not set, returns null.
If T is Set, this returns an unmodifiable set which will throw a runtime exception when mutated. Do not try to mutate the returned set.
Use MutablePreferences.set to change the value of a preference (inside a DataStore.edit block).
Parameters | |
---|---|
T |
the type of the preference |
key: Preferences.Key<T> | the key for the preference |
Exceptions | |
---|---|
ClassCastException |
if there is something stored with the same name as key but it cannot be cast to T |
hashCode
fun hashCode(): Int
minusAssign
operator fun minusAssign(key: Preferences.Key<*>): Unit
Removes the preference with the given key from this MutablePreferences. If this Preferences does not contain the key, this is a no-op.
Example usage: mutablePrefs -= COUNTER_KEY
Parameters | |
---|---|
key: Preferences.Key<*> | the key to remove from this MutablePreferences |
plusAssign
operator fun plusAssign(prefs: Preferences): Unit
Appends or replaces all pairs from prefs to this MutablePreferences. Keys in prefs will overwrite keys in this Preferences.
Example usage: mutablePrefs += preferencesOf(COUNTER_KEY to 100, NAME to "abcdef")
Parameters | |
---|---|
prefs: Preferences | Preferences to append to this MutablePreferences |
plusAssign
operator fun plusAssign(pair: Preferences.Pair<*>): Unit
Appends or replaces all pair to this MutablePreferences.
Example usage: mutablePrefs += COUNTER_KEY to 100
Parameters | |
---|---|
pair: Preferences.Pair<*> | the Preference.Pair to add to this MutablePreferences |
putAll
fun putAll(vararg pairs: Preferences.Pair<*>): Unit
Appends or replaces all pairs to this MutablePreferences.
Parameters | |
---|---|
vararg pairs: Preferences.Pair<*> | the pairs to append to this MutablePreferences |
remove
fun <T> remove(key: Preferences.Key<T>): T
Remove a preferences from this MutablePreferences.
Parameters | |
---|---|
key: Preferences.Key<T> | the key to remove this MutablePreferences |
Return | |
---|---|
the original value of this preference key. |
set
operator fun <T> set(
key: Preferences.Key<T>,
value: T
): Unit
Set a key value pair in MutablePreferences.
Example usage: val COUNTER_KEY = intPreferencesKey("counter")
// Once edit completes successfully, preferenceStore will contain the incremented counter. preferenceStore.edit { prefs: MutablePreferences -> prefs\[COUNTER_KEY\] = prefs\[COUNTER_KEY\] :? 0 + 1 }
Parameters | |
---|---|
key: Preferences.Key<T> | the preference to set |
key: Preferences.Key<T> | the value to set the preference to |