Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending a NSManagedObject's delete method

I'm using CoreData and was wondering if I could somehow extend my NSManagedObjects to do custom stuff (like deleting files belonging to this instance).

In my case I have a couple of CoreData entities and each entity may have a folder or file path stored in a property. If the entity gets deleted I want this entity to take care of deleting the referenced files on its own.

Such behavior would be particularly helpful in combination with the "Cascade" deletion rule as removing an entity removes all child entities and their files.

If there is no such mechanism I would write a Category for each entity containing a custom deletion method, including cascading behavior for entities that may have children.

What would you do, is there an elegant solution to my problem? Has this been discussed before and I just haven't found it?

Thanks a lot!

Paul

like image 409
Paul Avatar asked May 10 '12 09:05

Paul


1 Answers

You can do additional cleanup just before an object is deleted by overriding the prepareForDeletion method in your NSManagedObject subclass.

like image 108
omz Avatar answered Oct 06 '22 00:10

omz