ios - 使用 Core Data 进行排序的 fetchRequest 有哪些限制?

标签 ios cocoa sorting core-data nsfetchrequest


简而言之:

获取实体并根据不是属性@property对它们进行排序将导致错误(涉及NSDictionaryMapNode)第二次运行。

这是正常现象还是bug?您对此有什么意见或帮助吗?


长:

情况如下。

我有一个带有两个属性 attribute1attribute2Entity

我已经生成了[1]一个类Entity.m并向其添加了一个名为myProperty@property >。因此,myProperty 是我的类Entity.m@property,但不是实体Entity 的属性。顺便说一句,myProperty只读(假设它类似于 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] 使用 mogenerator (https://github.com/rentzsch/mogenerator)。

最佳答案

这是已知的。您无法对商店中不持久的任何内容进行排序。它永远不应该工作(第一次工作意味着你没有 MOC 或类似的东西)。

您应该向持久实体添加一个新属性。执行诸如实现 willSave 之类的操作或观察依赖键并使用该触发器来更新新属性 (myProperty) 的值。


请参阅 Core Data Programming Guide获取谓词和排序描述符部分

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

相关文章:

c++ - 将第 3 方 C++ 库添加到 Objective-C 项目中

objective-c - swift/Objective-C : How to call a view-controller-method from another class

ios - Game Center GKTurnBasedMatch 问题与 matchData 属性

arrays - 在数组中搜索一个值 [swift]

jquery - DataTables 根据 TD 属性值对 html 表进行排序

ios - 获取设备关闭时的当前时间

android - 如何构建一个简单的离线表单提交应用程序?

ios - 将字符串日期转换为 NSDate 的问题

objective-c - 捕获 NSEvent 以添加修饰符问题

java - 运行时错误。 java 。创建一个类 Sort,按升序显示四个整数