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

后端 未结 8 2247
你的背包
你的背包 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:19

    • commit() is synchronously, apply() is asynchronous

    • apply() is void function.

    • commit() returns true if the new values were successfully written to persistent storage.

    • apply() guarantees complete before switching states , you don't need to worry about Android component lifecycles

    If you dont use value returned from commit() and you're using commit() from main thread, use apply() instead of commit()

提交回复
热议问题