2

I have an NSManagedObject ElementA with several attributes that should exist in ElementB through a parent-child relationship. When setting ElementA as the Parent Entity to ElementB, the NSPersistentStoreCoordinator fails. The ManagedObjectModel is correctly being built, and the entities/classes work separate of each other. The only difference between the app failing and compiling is this parent-child relationship. None of the attributes from either entity overlap.

I don't have enough rep yet, so the images are at the following links: ElementA Model, ElementB Model.

As far as troubleshooting goes, I've tried all of the following:

  1. With and without implementing custom classes.
  2. Setting ElementA as abstract (however I need it to not be abstract)
  3. Removing and then adding in the attributes one at a time (including emptying all attributes of both entities)
  4. Resetting Xcode (clean), the simulator (reset all), and restarting my machine.

I've read up on Apple's Docs (Core Data Programming Guide: Managed Object Models) and everything seems to align with their guidelines for Entity Inheritance.

This is the line that fails:

__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

What do I seem to be missing here? It's got to be obvious as this does not seem like it should be this hard. Any and all help is appreciated!

Edit for @Rog's Comment

The application fails as soon as the core data model is accessed for the first time at startup. The new images above show that I am trying to set the Parent Entity of ElementB using the Model Editor. The following is the error message I'm receiving:

uncaught exception 'NSInternalInconsistencyException', reason: 'Bad model.  For entity 'ElementA' subentity 'ElementB (0x785d790)' is not registered in NSManagedModelModel.  Model has a reference to ElementB (0x785e320)'
sbonami
  • 1,892
  • 17
  • 33
  • You need to provide more details. Post a screenshot of your managed object model view. Also when is it crashing - during start up, when instantiating one of the managed objects. How are you setting ElementA as parent / superclass of ElementB? What is the error that you are getting? – Rog Feb 03 '12 at 06:12

2 Answers2

1

Not the full code... but this is how I achieved what @Scott BonAmi is talking about when removing the temporary entities. As I'm still using modelByMergingModels:, it figures out the sub entities itself.

NSMutableArray *finalModels = [NSMutableArray arrayWithCapacity:0];
NSMutableArray *updatedEntities = [NSMutableArray arrayWithCapacity:0];

for (NSManagedObjectModel *immutableModel in allModels)  {
    NSManagedObjectModel *model = [immutableModel mutableCopy];

    for (NSEntityDescription *entity in [model entities]) {
        if ([[[entity userInfo] objectForKey:@"TempPlaceholder"] boolValue])  {
            // Ignore placeholder.
            DULog(@"Ignoring: %@", entity.name);
        } else {
            [updatedEntities addObject:entity];
        }
    }

    [model setEntities:updatedEntities];
    [updatedEntities removeAllObjects];
    [finalModels addObject:model];
}

NSManagedObjectModel *model = [NSManagedObjectModel modelByMergingModels:finalModels];
bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
1

Ended up being a logic error with code I used from another SO answer creating the MOM dynamically.

When adding entities to the array during the looping sequence, ElementB (0x785d790) is added as a subentity of ElementA, and then later in the loop 'ElementB (0x785e320)' is added, thus causing different memory locations and throwing an NSInternalInconsistencyException.

Community
  • 1
  • 1
sbonami
  • 1,892
  • 17
  • 33
  • Do you have any more details on your workaround to this. I'm facing the same issue right now, and am about to implement a solution. – bandejapaisa Nov 19 '13 at 10:56
  • @bandejapaisa: When I get back to a machine that has the code on it, I'll post an edit to this answer. – sbonami Nov 19 '13 at 16:49