Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
I have two entities and two NSMangedObject subclasses. One entity, which we'll call Alice, has a to-many relationship with the other entity - which we'll call Bob. When an Alice object is created, it needs to have an associated Bob object so that my other code works.

Here's the code from my Alice subclass's awakeFromInsert method:
Code:
-(void)awakeFromInsert

{

    [super awakeFromInsert];

    Bob *bob = [[Bob alloc] initWithEntity:[self entity] insertIntoManagedObjectContext:[self managedObjectContext]];

    [self addPlacesObject:bob];

}
 

Mascots

macrumors 68000
Sep 5, 2009
1,667
1,418
Which is causing the BAD_ACCESS exception? At some point, you're accessing memory that either hasn't been allocated or has already been deallocated - it'll be faster to use a breakpoint to see exactly what memory doesn't exist.

If it's related to the code you've posted, it may be the [self entity], though I could be wrong because I believe both of the properties you are accessing should be available by the time awakeFromInsert is being called since they are required for the initializer. If you can verify those aren't set then I may be able to suggest other things that cause that problem - Otherwise, the error is probably being propagated by some kind of indirect response from elsewhere in your app which is not fun.

Maybe initializing your subclass an alternate way may help (copy with care, it's been awhile since I've written objc ;]):

Code:
bob = [NSEntityDescription insertNewObjectForEntityForName:@"Bob" inManagedObjectContext: [self managedObjectContext]];

Edit:
I also meant to say that I personally handle all relationship automation in willSave since I can guarantee the entities are normalized before being placed into the database. I find the end values matter more than the starting values, and if I were to require a relationship in the entity prior to working with it, I'd probably write a custom initializer in which my related entity has to be created in the same context (note: not saved) and passed with it to get the dependent one.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.