问题
Ok, I initially wanted to make NSSortDescriptor
of a request for NSFetchedResultsController
to sort based on the property in my NSManagedObject
subclass, but It obviously won't do it, because NSFetchedResultsController is limited to predicates and sort descriptors that work on the fetched entity and its relations, so I decided to create a transient attribute in my data model, synthesis the property for this attribute to ivar in my NSManagedObject
subclass, and sort based on it.
When running it, i got while executing fetch 'NSInvalidArgumentException', reason: 'keypath isActive not found in entity <NSSQLEntity SMSourceEntity id=2>'
I know this is KVO issue, so I have added + (NSSet*)keyPathsForValuesAffectingIsActive
, but still have the same issue.
What did I do wrong, or I'm still missing something to make it find my keypath? Thanks.
code:
@implementation SMSourceEntity
@dynamic friendlyName;
@dynamic interfaceAddress;
@dynamic uniqueID;
@dynamic network;
@synthesize isActive = _isActive;
+ (NSSet*)keyPathsForValuesAffectingIsActive
{
return [NSSet setWithObject:@"isActive"];
}
@end
my sortDescriptor:
request.sortDescriptors = [NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"isActive" ascending:NO] , nil];
回答1:
It isn't a KVO
issue, it's an issue with what you're trying to do because the FRC requires that the sort can be applied to the underlying SQLite store. In other words, you can only filter and sort on non-transient attributes. You will need to make the attribute non-transient so that it's value is saved into the store and available to SQLite.
For the FRC, only the section name key path attribute can be transient.
来源:https://stackoverflow.com/questions/17853496/nssortdescriptor-on-transient-attribute-for-nsfetchedresultscontroller