Delete an instance of a kernel data object - should the relationship really be set to zero?

Removing the main data object in iOS is really a common task, I would say. But I have a common problem that I just can’t understand myself: if the instance has a relationship, the deletion is not performed using NSValidationRelationshipDeniedDeleteError (1600). This is true even if all relationships, including the inverse ones, are set to "optional." Apple documentation says this error code means that "some relationships with the NSDeleteRuleDeny delete rule are not empty." Not so here; all relationships have a nullify deletion rule.

I even tried with the simplest attitude that I could think of using the example of an employee and the Apple documentation department. I can remove an employee if he does not have a department.

I found this thread about this problem; the solution must establish all relations to zero before saving the MOC. It really works, but it doesn’t make any sense to me.

My question is: is this really common practice? How does that make sense? What does everyone else do to delete Core Data objects with relationships?

PS: In case someone is interested, this is my code to clear the relationship; entityInstance is the instance to be deleted. The code is common to my application, that is, it clears all the relations of the object that needs to be deleted:

 NSEntityDescription *ed = entityInstance.entity; NSDictionary *relations = [ed relationshipsByName]; NSArray *keys = [relations allKeys]; [keys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { [entityInstance setValue:nil forKey:(NSString*) obj]; }]; 
+6
source share

Source: https://habr.com/ru/post/912169/


All Articles