What's the difference between commit() and apply() in SharedPreferences

后端 未结 8 2246
你的背包
你的背包 2020-11-22 10:03

I am using SharedPreferences in my android app. I am using both commit() and apply() method from shared preference. When I use AVD 2.3

8条回答
  •  遥遥无期
    2020-11-22 10:24

    The difference between commit() and apply()

    We might be confused by those two terms, when we are using SharedPreference. Basically they are probably the same, so let’s clarify the differences of commit() and apply().

    1.Return value:

    apply() commits without returning a boolean indicating success or failure. commit() returns true if the save works, false otherwise.

    1. Speed:

    apply() is faster. commit() is slower.

    1. Asynchronous v.s. Synchronous:

    apply(): Asynchronous commit(): Synchronous

    1. Atomic:

    apply(): atomic commit(): atomic

    1. Error notification:

    apply(): No commit(): Yes

提交回复
热议问题