ConcurrentModificationException in Android while accessing Shared Preferences

前端 未结 2 2346
萌比男神i
萌比男神i 2021-02-20 15:16

When I develop an android app, I run into the exception which I do not have any clue; I have googled related topics but none of them helped.

Fatal Exception: jav         


        
相关标签:
2条回答
  • 2021-02-20 15:55

    Preferences are thread safe(!), but not process safe. The answer of @mohan mishra simply not true, no need to synchronize everything. The problem here, as statet out in another question is, that per documentation you MUST NOT modify any instance that is returned by getStringSet and getAll

    getStringSet()

    Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.

    getAll()
    

    Note that you must not modify the collection returned by this method, or alter any of its contents. The consistency of your stored data is not guaranteed if you do.

    To the other question

    Documentation

    0 讨论(0)
  • 2021-02-20 16:04

    Please ensure that you are not accessing the preferences from any type of background thread. Also all your methods to add to preference must be synchronised(if you have your own preference managing class)

    0 讨论(0)
提交回复
热议问题