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

core-data, ios, many-to-many, objective-c

Solution

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];
}

Problem

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?

Original source