An alternative way to disable undo registration when using Core Data

As previously discussed, Core Data takes a little coaxing if you want to disable undo registration. The standard approach is:

The main point is to force through any changes the context has pending while the undo manager is still receptive, and then do the same before enabling registration again.

But what if you have other objects that have similar pending changes? Perhaps multiple MOCs attached to a single undo manager, or a custom class of your own. Or even just don't have a reference to the context itself handy. Well here's a simple alternative:

All managed object contexts observe their undo manager's checkpoint notification since that is the correct time for them to perform standard processing of pending changes. By posting the notification yourself, it's just an easy way to flush any changes from all relevant objects.

Customising NSArrayController arranged object removal

NSArrayController is quite possibly one of the most useful classes in Cocoa; it applies to so much situations in a modern application. Quite often you may need to subclass it to customise some aspect of the behaviour.

In particular, overriding -insertObject:atArrangedObjectIndex: is particularly handy. I use it regularly with Core Data so that inserting a managed object updates some kind of index attribute for sorting objects by. The documentation is on your side here, calling out the reason to override this method.

But what if you need to customise the behaviour when removing objects too? In the example above you could use it to update the index attribute of any remaining objects. Or your controller might be managing some unusual kind of relationship where removing an object requires extra work such as deleting unused objects.

If so, the docs are no longer on your side. No indication is given how you might do this – although there is a helpful note about the behaviour of -removeObject: with Core Data. Unsatisfied (yes, I filed rdar://7447617), I was forced to seek the answer by trial and error. So without further ado, here's the solution. Some simple drop-in code for your own custom classes:


© Mike Abdullah 2007-2012