简而言之:
提取实体并根据不是@property
的attribute
对其进行排序将在第二次运行时导致错误(涉及NSDictionaryMapNode
)。
是正常现象还是错误?您对此有何评论或帮助?
很长:
情况如下。
我有一个带有两个属性Entity
和attribute1
的attribute2
。
我已经生成了[1]一个类Entity.m
,并向其添加了一个@property
,称为myProperty
。因此,myProperty
是我的类@property
的Entity.m
,而不是实体Entity
的属性。顺便说一句,myProperty
是readonly
(假设它类似于attribute1
与attribute2
串联在一起。
现在,我执行以下操作:
NSManagedObjectContext * myContext = ... ;
NSFetchRequest * myRequest = [NSFetchRequest fetchRequestWithEntityName:@"Entity"];
NSSortDescriptor * mySortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"myProperty"
ascending:YES] ;
NSError *error ;
[myRequest setSortDescriptors:@[mySortDescriptor]] ;
NSArray * result = [myContext executeFetchRequest:myRequest
error:&error] ;
[<NSDictionaryMapNode 0x1020cf310> valueForUndefinedKey:]: this class is not key value coding-compliant for the key myProperty.
我了解问题出在
myProperty
而不是Entity
的属性。我的帖子的目的是提出这种情况,并知道您是否对此情况有任何评论。[1]使用发电机(https://github.com/rentzsch/mogenerator)。
最佳答案
这是众所周知的。您无法对商店中不存在的任何持久性进行排序。它永远都不会工作(第一次工作意味着您没有MOC或类似的东西)。
您应该将一个新属性添加到持久性实体。做类似实现willSave
的操作或观察相关键,然后使用该触发器更新新属性(myProperty
)的值。
参见Core Data Programming Guide的Fetch Predicates and Sort Descriptors
部分
关于ios - 使用Core Data进行排序的fetchRequest有什么限制?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19401055/