views:

28

answers:

1

I was expecting to find that in the NSAttributeDescription class, but only the default value is there.

Behind the scene I tought a validationPredicate was created but trying to reach it using

    NSDictionary* dico= [[myManagedObject entity] propertiesByName];
NSAttributeDescription* attributeDescription=[dico objectForKey:attributeKey];
for (NSString* string in [attributeDescription  validationWarnings])

just get me nowhere, no validationWarnings, no validationPredicates...

any thoughts on this ?

Edit1: It seems that getting the entity straight from the managedObject doesn't give you the full picture. Getting the Entity from the NSManagedObjectModel permits to reach the validationWarnings & validationPredicates...

Edit2:

Using the following code just after the creation of the model will list all the validationPredicates of the model. For some reasons, using it later (at the time I try too validate for example) will not display anything !

for (NSEntityDescription *entity in managedObjectModel) {
for (NSAttributeDescription* attributeDescription in [entity propertiesByName])
    for (NSPredicate* predicate in [attributeDescription validationPredicates])
    {
        NSLog(@"---%@",[predicate predicateFormat]);
    }

}
A: 

I think you problem is most likely caused by calling [entity propertiesByName] which returns dictionary of mixed NSAttributeDescription and NSRelationshipDescription. When I tried to run your code I got the odd result that the attribute objects where returned as NSCFStrings.

The following code works and note that it produces output even after the managed object model is frozen by use.

- (void) dumpAttributePredicates{

    NSLog(@"start dumpAttributePredicates");
    for (NSEntityDescription *entity in managedObjectModel) {
        NSDictionary *attribs=[entity attributesByName];
        for (NSAttributeDescription *eachA in [attribs allValues]) {
            NSArray *vps=[eachA validationPredicates];
            for (NSPredicate  *p in vps) {
                NSLog(@"p =%@",[p predicateFormat]);
            }
        }       
    }
    NSLog(@"end dumpAttributePredicates");
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
    [self dumpAttributePredicates];
    NSManagedObject *mo=[NSEntityDescription insertNewObjectForEntityForName:@"TestEntity" inManagedObjectContext:self.managedObjectContext];
    NSLog(@"mo=%@",mo);
    [self dumpAttributePredicates];
}

Which produces this output:

start dumpAttributePredicates
p =SELF >= 0
p =SELF <= 100
p =length >= 0
p =length <= 100
end dumpAttributePredicates

mo=<NSManagedObject: 0x10016f490> (entity: TestEntity; id: 0x10016f540 <x-coredata:///TestEntity/tA91445DF-4669-4018-A761-7383E3A73EF42> ; 
data: {
    attributeOne = 0;
    attributeTwo = nil;
})

start dumpAttributePredicates
p =SELF >= 0
p =SELF <= 100
p =length >= 0
p =length <= 100
end dumpAttributePredicates

Make sure you don't confuse entities with NSManagedObject or its subclasses. Entities are the analogs of classes, not instances and an entity only exist within a managed object model which in turn maybe an attribute of a managed object context.

TechZen