Android - Shared Preferences are lost sometimes

前端 未结 5 1500
北海茫月
北海茫月 2020-12-08 15:12

Some of the users of my application complain that sometimes (in a random way) the settings of my application are getting reverted to their default state (usually after a reb

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 15:56

    I'd echo the other answers - that you need to avoid conflicts if you don't want to corrupt the file - and I'd go further to suggest that you're probably misuing SharedPreferences.

    SPs are designed to store small pieces of information about your app - user settings like volume or whether music is playing or things like that.

    SPs are NOT designed for storing data which changes often and/or large amounts of data and it's a bad idea to try to do this (for the reasons you've discovered and a few others).

    Remember that SPs are really just an XML file - you're incurring the overhead of parsing and recreating that every time you change it too!

    The idea of an App which updates SPs in more than one thread is a bit mad I think - you need a better way of managing and storing the data you're saving - it will pay-off for you in more than one way...

提交回复
热议问题