Enumerating over managed object to-many relationships
Enumerating over managed object to-many relationships
- Subject: Enumerating over managed object to-many relationships
- From: Alexander Griekspoor <email@hidden>
- Date: Fri, 2 Nov 2007 14:02:42 +0000
Hi,
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?
And how would I solve this? I'm not sure if I can just copy the set
first (and if that helps). Or perhaps I should do [NSArray
arrayWithSet: authors] ?
I guess the best way would be to first store the authors that need to
be deleted and only after the enumeration I should delete them all.
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)?
Thanks for the insights,
Alex
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden