views:

79

answers:

1

In which situations would I want to call -processPendingChanges of NSManagedObjectContext?

A: 

The two cases where I've used -processPendingChanges have involved undo grouping and temporary disabling of undo.

For example, I wanted to group all Core Data actions involving the deletion of a particular item (which could lead to deletions of others or other changes to the data model), so I used code like the following:

[[[targetOperationController managedObjectContext] undoManager] beginUndoGrouping];
[targetOperationController deleteSelectedOperation];
[[targetOperationController managedObjectContext] processPendingChanges];
[[[targetOperationController managedObjectContext] undoManager] endUndoGrouping];

In order to guarantee that all data model changes triggered by the actions in the -deleteSelectedOperation method would be grouped together, I use -processPendingChanges to make sure they are all registered in the undo grouping block.

You can do something similar to ignore all data model changes for the purpose of undo by replacing the undo grouping with -disableUndoRegistration and -enableUndoRegistration.

Brad Larson