Skip navigation.
 
mlre: Enumerating over managed object to-many relationships
FROM : Ben Trumbull
DATE : Sun Nov 04 01:18:38 2007

> After compiling my app on Leopard I now run into a number of
> "Collection was mutated while being enumerated" exceptions.
> The problem is that it's not obvious why/how I am editing the
> collection and neither how to solve it the best way.
>
> Take this example:
>
>     // given a paper, get all its authors, iterate over them and delete
> the ones with just one paper
>     NSSet *authors = [paper valueForKey: @"authors"];
>     NSEnumerator *e = [authors objectEnumerator];
>     Author *author;
>     while(author = [e nextObject]){
>         if([[author valueForKey: @"papers"]count] == 1){
>             [author removePapersObject: paper];
>             [context deleteObject: author];
>         }
>     }
>     
> Do I indeed change the authors set ? Indirectly and ultimately yes,
> if I would ask for  [paper valueForKey: @"authors"] again I would get
> a different set, but does it refresh this everytime this set then?


You are changing the collection you are iterating over during the loop.

> Can someone clearly explains how the situation works, why the above
> is bad, and what the best way of doing this is (perhaps also showing
> the ObjC 2.0 way)?


Here's how I would write that on 10.5

   // given a paper, get all its authors, iterate over them and delete 
the ones with just one paper
   NSSet *authors = [[paper authors] copy];
   for(Author* a in authors) {
       if ([[a papers] count] == 1) {
           [a removePapersObject: paper];
           [context deleteObject: a];
       }
   }
        [authors release];


Although, depending on how you set up the delete rules in your model, 
you could let the framework do more of the work.  For example, authors 
might have a nullify delete rule to the papers, in which case you 
don't need to call -removePapersObject yourself.  Instead you can let 
if happen at the end of the event when the changes are coalesced and 
the deletions cascaded.  In that case, you wouldn't need to make a 
copy, since the side effects on the authors set happens outside the 
loop.

- Ben

Related mailsAuthorDate
mlEnumerating over managed object to-many relationships Alexander Griekspo… Nov 2, 15:02
mlRe: Enumerating over managed object to-many relationships I. Savant Nov 2, 15:07
mlre: Enumerating over managed object to-many relationships Ben Trumbull Nov 4, 01:18