Reputation: 101
I have a requirement where I am using a LinkedHashSet and converting it to json(I need unique elements and need to maintain order hence using LinkedHashSet). I need to take a lock on this collection so that when I am converting it to json, no other process/thread should be able to modify it. basically I want to avoid concurrent modification exception on the collection.
CoroutineScope(Dispatchers.IO).launch {
synchronized(linkedHashSet){
val jsonString = Gson().toJson(linkedHashSet)
sharedPreferences?.edit()?.putString("DataList", jsonString)?.apply()
}
}
I tried this but doesn't seem to work as i am still getting concurrent modification exception. Any other way?
Upvotes: 0
Views: 85