简而言之:

提取实体并根据不是@propertyattribute对其进行排序将在第二次运行时导致错误(涉及NSDictionaryMapNode)。

是正常现象还是错误?您对此有何评论或帮助?

很长:

情况如下。

我有一个带有两个属性Entityattribute1attribute2

我已经生成了[1]一个类Entity.m,并向其添加了一个@property,称为myProperty。因此,myProperty是我的类@propertyEntity.m,而不是实体Entity的属性。顺便说一句,myPropertyreadonly(假设它类似于attribute1attribute2串联在一起。

现在,我执行以下操作:

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 GuideFetch Predicates and Sort Descriptors部分

    关于ios - 使用Core Data进行排序的fetchRequest有什么限制?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19401055/

    10-11 22:01
    查看更多