为什么 NSArrayController 会“丢失”?插入对象后它的排序?
我有一个 NSArrayController 绑定到 NSManagedObject 子类的属性。子类由 mogenerator 自动生成(它为关系创建一个集合属性)。
NSArrayController 绑定到 network.posts,其中“posts”是 1-M 关系。请注意,我绑定到“posts”而不是由 mogenerator 生成的“postsSet”可变集访问器。
该集合表示与另一个实体的关系。阵列控制器具有排序描述符集,当与其关联的窗口打开时,数据会以正确的排序顺序显示。
然后,我通过实例化一个实体并将其添加到关系中来添加一个新对象。
NSArrayController 正确地观察到了这一变化,并且新对象出现在 arrangedObjects 中,但是,插入后,arrangedObjects 的排序顺序丢失,记录以不同的顺序出现。
我已经验证排序描述符仍然设置正确。 NSArrayController 具有 autoRearrangeContent=YES。我什至尝试在插入后手动调用 -rearrangeObjects 但排序顺序仍然错误。
如果我关闭窗口并重新打开,新实例化的 NSArrayController 再次具有正确排序顺序的数据。直到我再做一次插入。
我的经验是,当添加/删除对象时,NSArrayController 会自动保持正确的排序顺序,但也许这是一个幸运的巧合?
我在苹果文档中找不到任何关于正确行为的描述,所以我不知道会发生什么,可能会出现什么问题(如果有的话),或者 - 在任何情况下 - 我应该采取什么措施。
1)给定一个 sortDescriptor,在从底层集合中插入/删除对象后,NSArrayController 是否应该保持排列对象的排序?
2)如果是这样,什么可以防止这种情况发生?
3)如果不是,保持排列对象排序的正确方法是什么?
我将不胜感激任何帮助。在这种情况下提供有用的源代码并不容易,因为原则上不存在任何源代码。但我很乐意澄清并回答任何后续问题。
I have an NSArrayController bound to a property of an NSManagedObject subclass. The subclass is automatically generated by mogenerator (which creates a set property for the relationship).
The NSArrayController is bound to network.posts where 'posts' is a 1-M relationship. Note I am binding to 'posts' rather than the 'postsSet' mutableSet accessor generated by mogenerator.
The set represents a relationship with another entity. The array controller has a sort descriptor set and, when the window it is associated with opens, data is displayed in the correct sort order.
Then I add a new object by instantiating an entity and then adding it to the relationship.
The NSArrayController is correctly observing this change and the new object appears in arrangedObjects however, after the insert the sort order of arrangedObjects is lost and the records appear in a different order.
I have verified that the sort descriptor is still set correctly. The NSArrayController has autoRearrangeContent=YES. I've even tried manually calling -rearrangeObjects after the insert but the sort order remains wrong.
If I close the window and re-open the newly instantiated NSArrayController has the data with the correct sort order again. Until I do another insert.
My experience has been that NSArrayController has automatically kept the correct sort order when objects are added/removed but maybe that was a lucky coincidence?
I can't find any description of the correct behaviour in the Apple document so I've no idea what to expect, what might be going wrong (if anything), or - in either case - what I should do about it.
1) Given a sortDescriptor should NSArrayController be keeping arrangedObjects sorted after objects are inserted/removed from the underlying collection?
2) If so, what might prevent this from happening?
3) And, if not, what is the correct way to keep arrangedObjects sorted?
I'd be grateful for any help. It's not easy to provide useful source code in this situation since, in principle, there isn't any. But I'm happy to clarify and answer any follow-up questions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关闭延迟获取。不确定原因,但是当控制器很懒时,它在编辑或添加内容时不会使用它们。
Turn off lazy fetching. Not sure the reason but when the controller is being lazy it doesn't resort things when they are edited or added.
你按什么排序?核心数据不支持有序集合。要保留任何类型的顺序,您必须添加一些排序属性(双关语)并按其排序。
在幕后,CD 使用 NSSet/NSMutableSet(而不是 NSArray/NSMutableArray)来进行集合。您的对象多次以相同的顺序出现完全是由于缓存,而不是通过 CD 维护集合的顺序。
Lion (10.7) 更新
关于我的“不支持有序集合”声明:如果您在应用程序中定位 10.7 及更高版本,[NSManagedObject 现在为您提供有序关系。][1] 使用 -mutableOrderedSetValueForKey: 和 -mutableOrderedSetValueForKey: 设置和检索 NSOrderedSets。耶!
By what are you sorting? Core data does not support ordered collections. To preserve any kind of order, you have to add some sort of attribute (pun intended) and sort by it.
Behind the scenes, CD is using NSSet/NSMutableSet - not NSArray/NSMutableArray - for collections. That your objects come up in the same order more than once is entirely due to caching, not by CD maintaining your collection's order.
Update for Lion (10.7)
With regard to my "does not support ordered collections" statement: If you're targeting 10.7 and above in your application, [NSManagedObject now gives you ordered relationships.][1] Use -mutableOrderedSetValueForKey: and -mutableOrderedSetValueForKey: to set and retrieve NSOrderedSets. Yay!
也许您不应该将 NSArrayController 绑定到您的
attributesSet
(由 mogenerator 生成),而是应该绑定到由 Core Data 框架创建的底层动态attributes
属性本身?attributes
代表您的关系名称。Perhaps instead of binding the NSArrayController to your
attributesSet
(generated by mogenerator), you should instead bind to the underlying dynamicattributes
property itself, which is created by Core Data framework?attributes
stands for your relationship name.