berfis
berfis

Reputation: 409

Prevent deleting a Core Data NSManagedObject

I have a NSArrayController subclass which sets an instance variable of a newly inserted object and prevents the user to remove it if this variable is zero.

I'd like to do this inside an entity subclass. Setting the variable is possible in the awakeFromInsert method. Is there a way to prevent the deletion, a sort of:

if (self.testVariable == 0) return;   ?

I didn't find it, but maybe I missed something.

Upvotes: 0

Views: 303

Answers (2)

Gabe Rainbow
Gabe Rainbow

Reputation: 3738

based on a few tests, i believe the validateForDelete will not work to prevent deletion as desired.

validateForDelete is used to perform added validation prior to delete. essentially, the method name is misleading. Just like for example, for

moreover, prepareForDelete is called prior and will delete any Cascade relationships and mark null any Nullify ones. hence, those have to be 'backed out.' or alter your data model to only permit Deny that will leave those relationships alone in prepareForDelete. which is applying a stiff backhand to your business model and logic.

further, a Deny relationship is 'denied' when the user saves the data -- which might be too late for the user to undo and rectify the situation.

i believe the best option is to unactive the Remove Button that is bound to the canRemove method in the Controller.

Some solid opinions Some possible solutions at SO

Upvotes: 1

mprivat
mprivat

Reputation: 21902

Implement this in your NSManagedObject:

- (BOOL)validateForDelete:(NSError **)error

Upvotes: 1

Related Questions