I have a need to sort a CoreData table on one attribute in a table that needs 
to be derived from a calculation. I read about "Non-Standard Persistent 
Attributes" and did google and the only way I found to make it work is 
according to the following:

So, suppose I have an entity "Entity":

@implementation Entity

@dynamic attribute1;
@dynamic attribute2;
@synthesize positive;
@synthesize negative;
@dynamic net_count;

- (void)setPositive:(NSNumber *)newPositive
{
        [self willChangeValueForKey:@"positive"];
        positive = newPositive;
        [self didChangeValueForKey:@"positive"];
        [self willChangeValueForKey:@"net_count"];
        self.net_count = [NSNumber numberWithInteger:self.positive.integerValue 
- self.negative.integerValue];
        [self didChangeValueForKey:@"net_count"];
}

- (void)setNegative:(NSNumber *)newNegative
{
        [self willChangeValueForKey:@"negative"];
        negative = newNegative;
        [self didChangeValueForKey:@"negative"];
        [self willChangeValueForKey:@"net_count"];
        self.net_count = [NSNumber numberWithInteger:self. 
positive.integerValue - self.negative.integerValue];
        [self didChangeValueForKey:@"net_count"];
}

@end

That seems to work. Records appear in the right order.

Is there anything that I'm doing wrong with the above?

I initially tried to provide a dynamic value when net_count was called, e.g. 
implementing an accessor like:

- (NSNumber *)net_count
{
        return [NSNumber numberWithInteger:self.positive.integerValue - 
self.negative.integerValue];
}

But the records were not ordered properly.

-Laurent.
-- 
Laurent Daudelin
AIM/iChat/Skype:LaurentDaudelin                                 
http://www.nemesys-soft.com/
Logiciels Nemesys Software                                      
laur...@nemesys-soft.com


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to