Skip to content
Permalink
Browse files

Use SharedPreferences.Editor.apply() instead of commit()

  • Loading branch information...
pocorall committed Aug 6, 2018
1 parent b4fcbd0 commit 6060e8bc68fa020a1c4cbc7adb050163b4154769
@@ -176,14 +176,14 @@ abstract class PreferenceVar[T](val key: String, val defaultValue: T) {
def update(value: T)(implicit pref: SharedPreferences): this.type = {
val editor = pref.edit()
put(value, editor)
editor.commit()
editor.apply()
this
}

protected def put(value: T, editor: SharedPreferences.Editor): Unit

def remove()(implicit pref: SharedPreferences): this.type = {
pref.edit().remove(key).commit()
pref.edit().remove(key).apply()
this
}
}
@@ -58,12 +58,12 @@ import scala.reflect._
class Preferences(val preferences: SharedPreferences) extends Dynamic {
def updateDynamic(name: String)(value: Any) {
value match {
case v: String => preferences.edit().putString(name, v).commit()
case v: Int => preferences.edit().putInt(name, v).commit()
case v: Long => preferences.edit().putLong(name, v).commit()
case v: Boolean => preferences.edit().putBoolean(name, v).commit()
case v: Float => preferences.edit().putFloat(name, v).commit()
case v: Set[String @unchecked] => preferences.edit().putStringSet(name, v).commit()
case v: String => preferences.edit().putString(name, v).apply()
case v: Int => preferences.edit().putInt(name, v).apply()
case v: Long => preferences.edit().putLong(name, v).apply()
case v: Boolean => preferences.edit().putBoolean(name, v).apply()
case v: Float => preferences.edit().putFloat(name, v).apply()
case v: Set[String @unchecked] => preferences.edit().putStringSet(name, v).apply()
}
}

@@ -77,7 +77,7 @@ class Preferences(val preferences: SharedPreferences) extends Dynamic {
}

def remove(name: String) {
preferences.edit().remove(name).commit()
preferences.edit().remove(name).apply()
}

abstract class TypedPreferences[T] extends Dynamic {
@@ -145,14 +145,14 @@ abstract class PreferenceVar[T](val key: String, val defaultValue: T) {
def update(value: T)(implicit pref: SharedPreferences): this.type = {
val editor = pref.edit()
put(value, editor)
editor.commit()
editor.apply()
this
}

protected def put(value: T, editor: SharedPreferences.Editor): Unit

def remove()(implicit pref: SharedPreferences): this.type = {
pref.edit().remove(key).commit()
pref.edit().remove(key).apply()
this
}
}
@@ -25,13 +25,13 @@ import scala.reflect._
class Preferences(val preferences: SharedPreferences) extends Dynamic {
def updateDynamic(name: String)(value: Any) {
value match {
case v: String => preferences.edit().putString(name, v).commit()
case v: Int => preferences.edit().putInt(name, v).commit()
case v: Long => preferences.edit().putLong(name, v).commit()
case v: Boolean => preferences.edit().putBoolean(name, v).commit()
case v: Float => preferences.edit().putFloat(name, v).commit()
case v: String => preferences.edit().putString(name, v).apply()
case v: Int => preferences.edit().putInt(name, v).apply()
case v: Long => preferences.edit().putLong(name, v).apply()
case v: Boolean => preferences.edit().putBoolean(name, v).apply()
case v: Float => preferences.edit().putFloat(name, v).apply()
$if(ver.gte_11)$
case v: Set[String @unchecked] => preferences.edit().putStringSet(name, v).commit()
case v: Set[String @unchecked] => preferences.edit().putStringSet(name, v).apply()
$endif$
}
}
@@ -48,7 +48,7 @@ $endif$
}

def remove(name: String) {
preferences.edit().remove(name).commit()
preferences.edit().remove(name).apply()
}

abstract class TypedPreferences[T] extends Dynamic {

0 comments on commit 6060e8b

Please sign in to comment.
You can’t perform that action at this time.