In a managed object i have stored a path to an image file in the application\'s container.
When the managed object get’s deleted, the image file should be moved to trash
Excellent point - I updated my answer over on that question too, and I'll expand on my favorite approach here:
Forget about will/did save. Not reliable in this case.
Implement prepareForDeletion:
. If you don't need undo, and are sure the deletion will succeed, delete the file on the spot. Otherwise, add the file to a convenient registry (a NSMutableSet
owned by the context owner, or some such.)
If you do need undo/redo, implement awakeFromSnapshotEvents:
to catch un-deletion and re-deletion. Remove/re-add the file from/to the registry as needed.
Register for a didSave notification somewhere convenient. When a save happens, delete all the files listed in the registry and clear it.
This all assumes, by the way, that no two of your owning objects will ever own the same file. If this is a possibility, things get much more complicated - but I imagine you've set up your model specifically to avoid that happening.