I met the exception called ‘Collection was mutated while being enumerated’ yesterday and saw some solutions.I’ve got another idea.If I set an argument called ‘lock’ and each time I enumerate I lock it then unlock it after enumeration.Every time I’ll operate it,like enumerating or add object, I lock it first then unlock it after the operation.
Could someone tell me if it will work?
I met the exception called ‘Collection was mutated while being enumerated’ yesterday and saw
Share
I solved it.One of the problem is about thread safety.Use
that will work.It means that it’ll lock ‘yourobject’ so that other thread can’t modify it.Then unlock it after the ‘}’
The other is some code about deleting objects in array.If you delete instantly,you’ll meet the exception called ‘Collection was mutated while being enumerated’.Create a temp array to contain objects you want to delete,then delete them all after the for loop.
instead of