Skip to content

Commit

Permalink
Use SharedPreferences.Editor.apply() instead of commit()
Browse files Browse the repository at this point in the history
  • Loading branch information
pocorall committed Aug 6, 2018
1 parent b4fcbd0 commit 6060e8b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions scaloid-common/src/main/st/org/scaloid/common/helpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
14 changes: 7 additions & 7 deletions scaloid-common/src/main/st/org/scaloid/common/preferences.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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$
}
}
Expand All @@ -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 {
Expand Down

0 comments on commit 6060e8b

Please sign in to comment.