All those “NSOrderedSet was added later and thus doesn’t have to play nice with other components” bugs drive me crazy…
I have two managed objects and an ordered 1:N relationship between them, which is backed by an instance (or more precise a subclass) of NSOrderedSet
. I want to manage this relationship using a NSArrayController
in order to profit from features it offers (selection management, binding of the content, bindings to views like NSTableView
).
Since NSOrderedSet
is not a subclass of NSSet
, the contentSet
binding of NSArrayController
doesn't work with that relationship. I found following thread and tried to implement the suggestions mentioned there.
The first suggestion is to use the contentArray
binding and a value transformer for transforming the ordered set to an array on the fly. The problem with this solution is the reassigning of the content each time a change is made, which is not what I want.
The second suggestion provided in the mentioned thread is to use the contentArray
binding and apply the @array
operator to the model key path. I've tried that, but the underlying relationship was not touched at all when adding/removing objects through the NSArrayController
.
Another option I found is using sort descriptors with the contentSet
binding. This would require making the relation unordered in order to make the contentSet
binding work and introducing a new attribute used especially for managing the order. This would furthermore require a custom ordering mechanism to implement and it would mess up the model. Honestly said, I would like to avoid this solution.
My question is pretty clear: Is there any way to manage an ordered Core Data relationship using NSArrayController
? If so, which is the best way causing as little pain as possible?
It is indeed very sad that NSArrayController does not come with support for ordered relationships. As a keen observer to the bindings technology I find it sub optimal that it seems that Apple has "abandoned" it without saying anything. The last notable change that Apple introduced with regards to bindings are NSTreeController bug fixes. That was I believe with 10.6/10.7. It seems that Apple does not want to touch the bindings technology anymore. I am not sure why because bindings are really great sometimes. They can be the "90% solution". During prototyping this is fine. I am using bindings where it makes sense and having NSArrayController with ordered relationships support would be something great.
Most of the solutions that have been mentioned already are no real solutions. But this depends. Here is something to think about: