问题描述
好的,我最初想要 NSSortDescriptor
请求 NSFetchedResultsController
根据我的属性进行排序 NSManagedObject
子类,但显然不会这样做,因为NSFetchedResultsController仅限于对获取的实体及其关系起作用的谓词和排序描述符,所以我决定创建一个我的数据模型中的transient属性,在我的 NSManagedObject
子类中将此属性的属性合成为ivar,并根据它进行排序。
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.
运行时,我执行fetch 'NSInvalidArgumentException',原因:'在实体中找不到keypath isActive< NSSQLEntity SMSourceEntity id = 2>'
When running it, i got while executing fetch 'NSInvalidArgumentException', reason: 'keypath isActive not found in entity <NSSQLEntity SMSourceEntity id=2>'
我知道这是KVO问题,所以我添加了 +(NSSet *)keyPathsForValuesAffectingIsActive
,但仍然有同样的问题。
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.
代码:
@implementation SMSourceEntity
@dynamic friendlyName;
@dynamic interfaceAddress;
@dynamic uniqueID;
@dynamic network;
@synthesize isActive = _isActive;
+ (NSSet*)keyPathsForValuesAffectingIsActive
{
return [NSSet setWithObject:@"isActive"];
}
@end
my sortDescriptor:
my sortDescriptor:
request.sortDescriptors = [NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"isActive" ascending:NO] , nil];
推荐答案
这不是 KVO
问题,这是您尝试做的问题,因为FRC要求排序可以应用于底层SQLite存储。换句话说,您只能对非瞬态属性进行过滤和排序。您需要使属性非瞬态,以便将其值保存到存储中并可供SQLite使用。
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.
对于FRC,只有节名称键路径属性可能是短暂的。
For the FRC, only the section name key path attribute can be transient.
这篇关于NSFetchedResultsController的瞬态属性的NSSortDescriptor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!