Thursday, February 2, 2012

CoreData with NSOrderedSet does not work well on insertion

Usually, we generate an entity class with Editor->Create NSManagedOBject Subclass... For example we created a subclass of NSManagedObject

Xcode generates the .h file like this:

 @interface MyEntity : NSManagedObject  @property (nonatomic, retain) NSOrderedSet manySubs;  //... some methods generated by Xcode - (void)addManySubsObject:(MySubEntity*)value; //... some other methods generated by Xcode  @end  

and the .m file like this:

 @implement MyEntity  @dynamic manySubs;  @end  

If you use MyEntity just like this, you will get an error when you try to insert a new MySubEntity class by calling [myEntityObj addManySubsObject:aSubEntityObj];.

The error reads:

*** -[NSSet intersectsSet:]: set argument is not an NSSet

This is a bug Apple hasn’t resolved yet, but you can work around this bug by adding the following method to .m file.

 - (void)addManySubsObject:(MySubEntity*)value{     [self willChangeValueForKey:@"manySubs"];     NSMutableOrderedSet *tempSet = [NSMutableOrderedSet orderedSetWithOrderedSet:self.manySubs];     [tempSet addObject: value];     self.manySubs = tempSet;     [self didChangeValueForKey:@"manySubs"]; }  

Be careful



Via Tonny Xu http://feedproxy.google.com/~r/tonnyxu/~3/wO0dQNbn3JA/ iPhone Dev, Programming

No comments:

Post a Comment