Hi everyone
I have a bunch of apps with pretty much the same coredata structure. A lot of common code can be placed in a subclass of NSManagedObject (CommonManagedObject). Seems ideal for a framework. This works fine, but now I want to verify during startup if the NSManagedObjectModel is implemented correctly. Every entity that uses CommonManagedObject, either directly or via a subclass, should have the same minimum set of common properties (e.g. "title").
The code below works fine and I can use the CommonManagedObject and framework. I can identify entities that have "CommonManagedObject" as class.
However I don't know how to identify those entities that are a subclass of CommonManagedObject. "CommonManagedObject" in this is case is nowhere defined in the model and therefore doesn't show up.
It also doesn't show up via the superentity since these particular entities have no superentity.
I can determine the class of the entity but the code crashes when trying to determine the superclass.
This all happens when the app hasn't finished loading yet. So there is no NSManagedObjectContext yet or any other instances.
Thanks!
I have a bunch of apps with pretty much the same coredata structure. A lot of common code can be placed in a subclass of NSManagedObject (CommonManagedObject). Seems ideal for a framework. This works fine, but now I want to verify during startup if the NSManagedObjectModel is implemented correctly. Every entity that uses CommonManagedObject, either directly or via a subclass, should have the same minimum set of common properties (e.g. "title").
The code below works fine and I can use the CommonManagedObject and framework. I can identify entities that have "CommonManagedObject" as class.
However I don't know how to identify those entities that are a subclass of CommonManagedObject. "CommonManagedObject" in this is case is nowhere defined in the model and therefore doesn't show up.
It also doesn't show up via the superentity since these particular entities have no superentity.
I can determine the class of the entity but the code crashes when trying to determine the superclass.
This all happens when the app hasn't finished loading yet. So there is no NSManagedObjectContext yet or any other instances.
Thanks!
Code:
+(void) load
{
[super load];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
if (![CommonManagedObject verifyManagedObjectModel:model])
NSLog(@"Incorrect use of framework. NSManagedObjectModel does not adhere to requirements for CommonManagedObject");
[pool release];
}
+(BOOL) verifyManagedObjectModel:(NSManagedObjectModel *) model
{
BOOL result = TRUE;
NSArray *allEntities = [model entities];
[allEntities enumerateObjectsWithOptions:NSEnumerationConcurrent
usingBlock:^(id entityDescription, NSUInteger idx, BOOL *stop) {
NSString *entityClassName = [(NSEntityDescription *)entityDescription managedObjectClassName];
if (![entityClassName isEqualToString:@"NSManagedObject"])
{
if ([entityClassName isEqualToString:@"CommonManagedObject"])
{
NSLog(@"verify: %@",[entityDescription name]);
}
else
{
#if 0 //crashes
Class superClass = class_getSuperclass(NSClassFromString(entityClassName));
if ([NSStringFromClass(superClass) isEqualToString:@"CommonManagedObject"])
{
NSLog(@"verify: %@",[entityDescription name]);
}
#endif
}
}
}];
return result;
}
Last edited: