Re: CoreData: sorting on a computed attribute
Hi Laurent, when I faced a similar problem - in my case I needed to filter data acquired fromCoreData in a TableView for a field value that wasn't part of the model, but existed as an ivar in the NSManagedObject subclass - I used the following: [self.sessionController setFilterPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) { return [(Session*)evaluatedObject fileLocationInvalid]; }]]; Depending on the complexity of the calculation and the impact on performance you may want to do similar. You may be able to produce a similar result by using a block as comparator function for the descriptor. Or write your own comparator function to compare the calculated values. In both cases you won't need to create a persistent attribute, but do the calculation in the sort descriptor block or compare function. My 2cents, Volker Am 14.03.2013 um 18:11 schrieb Laurent Daudelin: 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: ___ 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
CoreData: sorting on a computed attribute
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