I'm working on some code that uses an NSOperation
to import data. I'd like for the user to be able to undo the NSManagedObject
instances that are created during the import operation.
From what I can tell, it's impossible to use the NSManagedObjectContext
-undoManager
for any operations that are performed off of the main thread. From the Core Data Programming Guide section on Use Thread Confinement to Support Concurrency, we have these two conditions:
- Only objectID should be passed between managed object contexts (on separate threads)
- Managed objects must be saved in a context before the objectID can be used.
This makes sense since the managed objects need to be moved from private storage (NSManagedObjectContext
) to public storage (NSPersistentStore
) before they can be shared.
Unfortunately, the -save:
message also causes any managed objects in the undo stack to be removed. From the Memory Management Using Core Data section of the same guide:
Managed objects that have pending changes (insertions, deletions, or updates) are retained by their context until their context is sent a save:, reset , rollback, or dealloc message, or the appropriate number of undos to undo the change.
I've tried several things to work around this limitation, and everything eventually leads back to bulk of the work happening on the main thread (and spinning beach balls.) Any clues to getting undo working with objects created off the main thread would be very much appreciated.
--
An enhancement Radar has been submitted: rdar://problem/8977725