views:

872

answers:

3

I followed the Core Data tutorial at

http://macresearch.org/cocoa-scientists-part-xxiii-itunes-ifying-core-data-app

The finished application displays data in an NSTableView. Is the ordering of the data persistent as well? And is there a way that you can allow drag and drop rearrangements to the order of the records? I am thinking about something that resembles how iTunes allows you to rearrange songs in a playlist with drag and drop.

A: 

You'll need to add a field that keeps track of the order of items (could be an integer field). When you put the objects in the table, you'll want to sort by that field.

When you implement the drag and drop, you should update the ordering of items.

wbyoung
+3  A: 

Core Data expects you to define your own sorting with NSSortDescriptors, so you'll have to add your own attribute to track a user-customizable ordering index like that. I've had luck using KVO on the entity's relationship keypath (from within the managed object itself) to change the ordering as needed when new relationship objects are added or removed.

To handle rearrangement, you'll want to take advantage of NSTableView's drag and drop methods and reset the order index attribute as needed. The NSTableView documentation should explain it pretty well.

Marc Charbonneau
A: 

Instead of using integer I use the actual date and time (kind of timestamp) of the creation. This will make sure that your order field is not duplicated.

Then when I move an item up/down I just swap their order field. It has a relatively low cost of processing.

When I fetch the data I just select the items in the order of the am field.

Teddy
This works only if you swap two consecutive elements.
Kamchatka