ConcurrentModificationException in Android when accessing general settings

When I develop an application for Android, I come across the exception that I do not have. I have Google related topics, but none of them helped.

Fatal Exception: java.util.ConcurrentModificationException java.util.HashMap$HashIterator.nextEntry (HashMap.java:806) java.util.HashMap$KeyIterator.next (HashMap.java:833) com.android.internal.util.XmlUtils.writeSetXml (XmlUtils.java:298) com.android.internal.util.XmlUtils.writeValueXml (XmlUtils.java:447) com.android.internal.util.XmlUtils.writeMapXml (XmlUtils.java:241) com.android.internal.util.XmlUtils.writeMapXml (XmlUtils.java:181) android.app.SharedPreferencesImpl.writeToFile (SharedPreferencesImpl.java:596) android.app.SharedPreferencesImpl.access$800 (SharedPreferencesImpl.java:52) android.app.SharedPreferencesImpl$2.run (SharedPreferencesImpl.java:511) java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1112) java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:587) java.lang.Thread.run (Thread.java:841) 
+7
source share
2 answers

Preferences are thread safe (!), But not safe for the process. @Mohan mishra's answer is simply not true, you do not need to synchronize everything. The problem here, as stated in another question, is that in each documentation, NO instance returned by getStringSet and getAll SHOULD be changed.

getStringSet()

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

 getAll() 

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

To another question

Documentation

+4
source

Please make sure that you are not accessing settings from any type of background stream. It is also necessary to synchronize all your methods to add to your preference (if you have your own preference management class)

+1
source

All Articles