I've got a sample project at
https://github.com/ericgorr/LazyFetching
The setup is pretty straightforward. In my CoreData model, I have two entities:
- Shelf which has a name property and a to-many relationship (items) to the Item entity
- Item which has a name property has a relationship to the Shelf entity (it's "parent")
I then have two NSArrayControllers:
Shelf Array Controller with the Mode 'Entity Name', the Entity Name of 'Shelf'. It Prepares Content and using Lazy Fetching.
Item Array Controller with the Mode 'Entity Name', the Entity Name of 'Item'. It does not prepare content and does not use lazy fetching. It's Content Set is bound to the Shelf Array Controller's selection and 'items' property.
On the Window, I've got two NSTableViews and two + buttons, each ties to one of the array controllers.
What I did was to:
- Press the top + button and named the new Shelf entry 'a'
- Press the top + button again and named the Shelf entry 'b'
- Pressed the top column header to sort the Shelf entries in ascending order.
- Make sure the 'b' entry is selected
- Pressed the bottom + button and renamed the Item entry to anything
What I see happen then is the rows in the upper (Shelf) table change order so that the 'b' entry is listed first.
If I press the Info button, what it will do is:
- NSLog the sort descriptors for the Shelf Array Controller
- Print the names of the entries in the arrangedObjects array of the Shelf Array Controller
- Call rearrangeObjects on Shelf Array Controller
- Print out the names of the entries in the arrangedObjects array of the Shelf Array Controller
After all of this, I see, as expected, a single sort descriptor on the Shelf Array Controller: name, ascending, compare:
And the following output for arrangedObjects:
2013-05-19 16:08:56.023 LazyFetching[11791:303] b
2013-05-19 16:08:56.024 LazyFetching[11791:303] a
and
2013-05-19 16:08:56.024 LazyFetching[11791:303] b
2013-05-19 16:08:56.024 LazyFetching[11791:303] a
For some reason, it is no longer sorting the items in the Shelf Array Controller.
If I turn off Lazy Fetching for the Shelf Array Controller, everything seems to work correctly.
So, what I don't understand is why I am seeing this behavior with Lazy Fetching enabled. It seems possible that it is a bug. If so, I can certainly file what is likely to be a duplicate report. But, I am guessing, there is some good explanation and a standard way of dealing with it so items remain sorted by the sort descriptors in the arrangedObjects array of the array controller.