On my Lion app, I have this data model:
The relationship subitems
inside Item
is ordered.
Xcode 4.1 (build 4B110) has created for me the file Item.h
, Item.m
, SubItem.h
and SubItem.h
.
Here is the content (autogenerated) of Item.h
:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class SubItem;
@interface Item : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSOrderedSet *subitems;
@end
@interface Item (CoreDataGeneratedAccessors)
- (void)insertObject:(SubItem *)value inSubitemsAtIndex:(NSUInteger)idx;
- (void)removeObjectFromSubitemsAtIndex:(NSUInteger)idx;
- (void)insertSubitems:(NSArray *)value atIndexes:(NSIndexSet *)indexes;
- (void)removeSubitemsAtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectInSubitemsAtIndex:(NSUInteger)idx withObject:(SubItem *)value;
- (void)replaceSubitemsAtIndexes:(NSIndexSet *)indexes withSubitems:(NSArray *)values;
- (void)addSubitemsObject:(SubItem *)value;
- (void)removeSubitemsObject:(SubItem *)value;
- (void)addSubitems:(NSOrderedSet *)values;
- (void)removeSubitems:(NSOrderedSet *)values;
@end
And here is the content (autogenerated) of Item.m
:
#import "Item.h"
#import "SubItem.h"
@implementation Item
@dynamic name;
@dynamic subitems;
@end
As you can see, the class Item
offers a method called addSubitemsObject:
. Unfortunately, when trying to use it in this way:
Item *item = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:self.managedObjectContext];
item.name = @"FirstItem";
SubItem *subItem = [NSEntityDescription insertNewObjectForEntityForName:@"SubItem" inManagedObjectContext:self.managedObjectContext];
[item addSubitemsObject:subItem];
this error appear:
2011-09-12 10:28:45.236 Test[2002:707] *** -[NSSet intersectsSet:]: set argument is not an NSSet
Can you help me?
Update:
After just 1,787 days from my bug report, today (August 1, 2016) Apple wrote me this: "Please verify this issue with the latest iOS 10 beta build and update your bug report at bugreport.apple.com with your results.". Let's hope this is the right time :)
Personally I have just replaced the calls to the CoreData generated methods with direct calls to the method as outlined in another solution by @Stephan:
This removes the need for categories that might later conflict with a solution from Apple to the generated code when the bug is fixed.
This has the added plus of being the official way to do it!
It seems that if you link the parent with the child by setting the parent to the child and not the other way around it works without crashing.
So if you do:
instead of
It should work, at least it works on iOS 7 and didn't had any problems with the relationship.
I have the same situation with an item called "signals" instead of "subitems". The solution with tempset works in my testing. Further, I had a problem with the removeSignals: method. This override seems to work:
If there is a better way to do this, please let me know. My values input is never more than 10 -20 items so performance isn't much of a concern - nonetheless please point out anything relevant.
Thanks,
Damien
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 just fell foul of this issue, and resolved it using a much simpler implementation than the others outlined here. I simply make use of the methods available on
NSManagedObject
for dealing with relationships when not using subclasses.An example implementation for inserting an entity into an
NSOrderedSet
relationship would look like this:This works perfectly, and is what I was using before I moved to
NSManagedObject
subclasses.I reproduced your setup both with your data model and one of my own with different names. I got the same error in both cases.
Looks like a bug in Apple's autogenerated code.