CoreData: sorting on a computed attribute
CoreData: sorting on a computed attribute
- Subject: CoreData: sorting on a computed attribute
- From: Laurent Daudelin <email@hidden>
- Date: Thu, 14 Mar 2013 10:11:59 -0700
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 email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden