I am encountering a ConcurrentModificationException
for the following:
@MainThread
private void notifyAdditionalItemsLoaded(
final @Nullable List<RandomItem> newAdditionalItems) {
if (newAdditionalItems != null) {
eventListener.onAdditionalItemsLoaded(
new ArrayList<>(newAdditionalItems));
}
}
The stack trace for the exception is:
java.util.ConcurrentModificationException
at java.util.ArrayList$SubList.size(ArrayList.java:1057)
at java.util.AbstractCollection.toArray(AbstractCollection.java:139)
at java.util.ArrayList.<init>(ArrayList.java:191)
at com.dash.module.component.model.datasource.notifyAdditionalItemsLoaded(datasource.java:434)
at com.dash.module.component.model.datasource.access$2300(datasource.java:42)
at com.dash.module.component.model.datasource$LoadAdditionalRecordsTask.onPostExecute(datasource.java:599)
at com.dash.module.component.model.datasource$LoadAdditionalRecordsTask.onPostExecute(datasource.java:540)
at com.dash.util.BreadcrumbTask.onPostExecute(BreadcrumbTask.java:43)
at android.os.AsyncTask.finish(AsyncTask.java:771)
at android.os.AsyncTask.-$$Nest$mfinish(Unknown Source:0)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:788)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8757)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
As seen with the stack trace, notifyAdditionalItemsLoaded()
is called from an async task's onPostExecute()
method which simply sets the processing task to null
and calls the notify..
method:
@Override
protected void onPostExecute(@Nullable List<RandomItem> items) {
loadAdditionalItemsTask = null; // reset
notifyAdditionalItemsLoaded(items);
}
And the items
list is initialized within task, where new RandomItem
objects are initialized and added to the list.
But, I am not sure why the CME is being thrown for shallow copying of objects to the list. And, this is not consistently reproducing.
Any help here would be very much appreciated. TYIA!