NSSortDescriptor on transient attribute for NSFetchedResultsController

纵饮孤独 提交于 2019-12-19 05:49:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!