David
David

Reputation: 2810

Core Data retrieve NSManagedObject attribute from within subclass

I have a core data entity and I've created a NSManagedObject subclass. Say it has a attribute, "attrib1" within entity "List". In my subclass, in some cases I'd like to calculate the value of attrib1, in other cases I'd like to return the value from the database. I'm trying to figure out how to return the database value from within my method. Example:

- (NSString *)attrib1 {
   if (flag) {
       return [self calculateValue];
   } else {
       // return value from core data, ie pass thru, but how?
       // Attempt1:
       return [super attrib1]; // Fails with 'unrecognized selector'
       // Attempt2. Ends up calling this method again, recursion loop
       return [super performSelector:@selector(attrib1)];
   }
}

How should I retrieve the value of the attribute "attrib1" from within the NSManagedObject subclass method which is the getter for attrib1.

Thanks

Upvotes: 0

Views: 170

Answers (1)

thom_ek
thom_ek

Reputation: 671

First, it's better to make other property that will return calculated value or fetch it from database - in this case attrib1 should be left untouched. You are also missing some implementation of getter. So, example code should look like this:

-(NSString *)attrib1 {
    NSString *v;
    [self willAccessValueForKey:@"attrib1"];
    if(flag)
        v=[self calculateValue];
    else
        v=[self primitiveAttrib1];
    [self didAccessValueForKey:@"attrib1"];
    return v;
}

Also, see Apple doc: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdAccessorMethods.html.

Upvotes: 1

Related Questions