Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an object to an NSSet in a Core Data with many-to-many relationships

I have a Core Data structure with Restaurants and Atmospheres, where a restaurant can have many atmospheres and an atmosphere can have many restaurants. So I made two to-many relationships, both being the inverse of themselves as stated in Apple's Documentation, forming a many-to-many relationship.

However I am having troubles adding objects to the sets created. Example, when I use code such as this one,

Atmosphere *atmosphere = [Atmosphere atmosphere:aId inManagedObjectContext:context];
[restaurant addAtmospheresObject:atmosphere];

it crashes with a weird error:

EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)

Has anyone every encountered this please?

like image 483
ChrisBorg Avatar asked Apr 07 '12 16:04

ChrisBorg


1 Answers

Looks like you are not creating your atmosphere object correctly. Try this:

Atmosphere *atmosphere = [NSEntityDescription 
   insertNewObjectForEntityForName:@"Atmosphere" 
            inManagedObjectContext:context];
// further configuration
if (restaurant) {
   [restaurant addAtmospheresObject:atmosphere];
}
like image 158
Mundi Avatar answered Oct 13 '22 01:10

Mundi