On my Lion app, I have this data model:
The relationship subitem
I just got the problem in Swift (Xcode 6.1.1).
The answer was DO NOT CODE ANY METHOD OR ADDITIONAL THINGS in your NSManagedObject subclasses. I think it is a compilator mistake. Very strange bug ..
Hope it helps ..
I'm quite sure it is finally fixed in iOS 10 beta 6!
If you are using mogenerator, then instead of
[parentObject add<Child>sObject:childObject];
simply use:
[[parent object <child>sSet] addObject:childObject];
I agree that there may be a bug here. I've modified the implementation of the add object setter to append correctly to a NSMutableOrderedSet.
- (void)addSubitemsObject:(SubItem *)value {
NSMutableOrderedSet* tempSet = [NSMutableOrderedSet orderedSetWithOrderedSet:self.subitems];
[tempSet addObject:value];
self.subitems = tempSet;
}
Reassigning the set to self.subitems will ensure that the Will/DidChangeValue notifications are sent.
Yes, this is definitely a Core Data bug. I wrote up an ObjC-Runtime-based fix a while back, but at the time I figured it would be fixed soon. Anyway, no such luck, so I posted it up on GitHub as KCOrderedAccessorFix. Work around the problem on all your entities:
[managedObjectModel kc_generateOrderedSetAccessors];
One entity in particular:
[managedObjectModel kc_generateOrderedSetAccessorsForEntity:entity];
Or just for one relationship:
[managedObjectModel kc_generateOrderedSetAccessorsForRelationship:relationship];
The Apple docs To Many Relations says: you should access the proxy mutable set or ordered set using
NSMutableOrderedSet * set = [managedObject mutableOrderedSetValueForKey:@"toManyRelation"];
Modifying this set will add or remove relations to your managed object. Accessing the mutable ordered set using the accessor whether with [ ] or . notation is wrong and will fail.