I'm working through a learning project around Core Data on OS X. I have an entity (Foo) in the data store, and in the UI, I use an NSArrayController with bindings to put some (name) field of the Foo objects into an NSTableView, where the user can add/delete/rename (and potentially reorder) the items.
As you know, Core Data storage is unordered. However, I'd like to give it order from the user standpoint. E.g. adding a new row to the table view fixes that new Foo to the end of the list, etc. If the table allows reordering, that order is maintained.
I assume that I should accomplish this by adding some hidden (ie the user never encounters it) "ordinal" field to the Foo entity, which is fine. I'm trying to figure out:
- Where is the right place to set this "ordinal" field for new items? Right now, a button is invoking the
add:
action on the array controller and bindings handles all the rest magically. (I'll need access to the count of items to set it correctly.) - Where is the right place to fixup the whole result set's ordinals on delete or reorder?
- Assuming all objects in the store have a reasonable ordinal attribute, what must I do to the UI connections to get the table view to display the results sorted by that ordinal? Is this something I can do purely with bindings? Do I need to subclass... something?
It feels like this is a common enough scenario that people here might have a ready answer. I'm new to Core Data and bindings-- trying to grope my way around. Don't need code; pointers in the right direction would be great. Thanks.