Discussion:
Getting ConcurrentModificationException, how to deal with it?
Marco Pas
2011-01-07 09:22:00 UTC
Permalink
Hi there,

when looping though a collection a get a java.util.ConcurrentModificationException on the code below.

The idea is that i want to remove the relation between all 'contacts' inside a 'contactgroup'. This is a many to many relationship.

contactGroup?.contacts?.each{ contact ->
contact.removeFromContactGroups(contactGroup).save(flush:true)
}

I am not removing any contacts but only the relationship, how can i avoid the java.util.ConcurrentModificationException ?

-- Marco



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Marcin Erdmann
2011-01-07 09:46:28 UTC
Permalink
Hi,

Marco you're modifying a collection while iterating over it hence the
exception. The easiest solution seems to be to first find the contacts you
want to remove using the findAll method and them iterate over the result
collection and remove each contact from contactGroup. Maybe there is a
better solution but this one should work for you.

Marcin
Post by Marco Pas
Hi there,
when looping though a collection a get a
java.util.ConcurrentModificationException on the code below.
The idea is that i want to remove the relation between all 'contacts'
inside a 'contactgroup'. This is a many to many relationship.
contactGroup?.contacts?.each{ contact ->
contact.removeFromContactGroups(contactGroup).save(flush:true)
}
I am not removing any contacts but only the relationship, how can i avoid
the java.util.ConcurrentModificationException ?
-- Marco
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
Marco Pas
2011-01-07 10:21:07 UTC
Permalink
Marcin,

Thanks for the clear explanation, at the end it seems so logical :)

-- Marco
Hi,
Marco you're modifying a collection while iterating over it hence the exception. The easiest solution seems to be to first find the contacts you want to remove using the findAll method and them iterate over the result collection and remove each contact from contactGroup. Maybe there is a better solution but this one should work for you.
Marcin
Hi there,
when looping though a collection a get a java.util.ConcurrentModificationException on the code below.
The idea is that i want to remove the relation between all 'contacts' inside a 'contactgroup'. This is a many to many relationship.
contactGroup?.contacts?.each{ contact ->
contact.removeFromContactGroups(contactGroup).save(flush:true)
}
I am not removing any contacts but only the relationship, how can i avoid the java.util.ConcurrentModificationException ?
-- Marco
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
zyro
2011-01-07 09:52:49 UTC
Permalink
def tmp = []
tmp.addAll contactGroup.contacts
tmp.each {
it.removeFromContactGroups(contactGroup)
it.save(flush:true)
}
--
View this message in context: http://grails.1312388.n4.nabble.com/Getting-ConcurrentModificationException-how-to-deal-with-it-tp3178826p3178865.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Marco Pas
2011-01-07 10:22:33 UTC
Permalink
Small simple and elegant solution, thanks for the reply. I surely appreciate it!

- Marco
Post by zyro
def tmp = []
tmp.addAll contactGroup.contacts
tmp.each {
it.removeFromContactGroups(contactGroup)
it.save(flush:true)
}
--
View this message in context: http://grails.1312388.n4.nabble.com/Getting-ConcurrentModificationException-how-to-deal-with-it-tp3178826p3178865.html
Sent from the Grails - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Loading...