MutablePreferences



Mutable version of Preferences. Allows for creating Preferences with different key-value pairs.

Summary

Public functions

open Map<Preferences.Key<*>, Any>

Retrieve a map of all key preference pairs.

Cmn
Unit
Cmn
open operator Boolean
<T : Any?> contains(key: Preferences.Key<T>)

Returns true if this Preferences contains the specified key.

Cmn
open operator Boolean
equals(other: Any?)
Cmn
open operator T?
<T : Any?> get(key: Preferences.Key<T>)

Get a preference with a key.

Cmn
open Int
Cmn
operator Unit

Removes the preference with the given key from this MutablePreferences.

Cmn
operator Unit

Appends or replaces all pair to this MutablePreferences.

Cmn
operator Unit

Appends or replaces all pairs from prefs to this MutablePreferences.

Cmn
Unit
putAll(vararg pairs: Preferences.Pair<*>)

Appends or replaces all pairs to this MutablePreferences.

Cmn
T
<T : Any?> remove(key: Preferences.Key<T>)

Remove a preferences from this MutablePreferences.

Cmn
operator Unit
<T : Any?> set(key: Preferences.Key<T>, value: T)

Set a key value pair in MutablePreferences.

Cmn
open String

For better debugging.

Cmn

Inherited functions

From androidx.datastore.preferences.core.Preferences
MutablePreferences

Gets a mutable copy of Preferences which contains all the preferences in this Preferences.

Cmn
Preferences

Gets a read-only copy of Preferences which contains all the preferences in this Preferences.

Cmn

Public functions

asMap

open 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.

Returns
Map<Preferences.Key<*>, Any>

a map containing all the preferences in this Preferences

clear

fun clear(): Unit

contains

open operator fun <T : Any?> 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

equals

open operator fun equals(other: Any?): Boolean

get

open operator fun <T : Any?> 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 : Any?>

the type of the preference

key: Preferences.Key<T>

the key for the preference

Throws
kotlin.ClassCastException

if there is something stored with the same name as key but it cannot be cast to T

hashCode

open 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(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

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

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 : Any?> remove(key: Preferences.Key<T>): T

Remove a preferences from this MutablePreferences.

Parameters
key: Preferences.Key<T>

the key to remove this MutablePreferences

Returns
T

the original value of this preference key.

set

operator fun <T : Any?> 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

toString

open fun toString(): String

For better debugging.