Purpose: I want to check the managed entity to make sure that it is ok to delete
Pending: -[NSManagedObject validateForDelete:] should return BOOLbased on the configuration of deletion rules in the managed object model
Class reference NSManagedObject
validateForDelete:
Determines whether the receiver can be deleted in the current state.
- (BOOL)validateForDelete:(NSError **)error
Parameters
Mistake
If the recipient cannot be deleted in its current state, it returns an NSError instance on return, which describes the problem.
Return value
YESif the receiver can be deleted in the current state, otherwise NO.
Discussion
An object cannot be deleted if it has a relationship, has a deny deletion rule, and that relationship has a destination.
ImplementationNSManagedObject s sends a description of the recipient entity to a message that performs a basic check based on the presence or absence of values.
Reading this, I guess I should do something like this:
BOOL canDelete = [myManagedObject validateForDelete:&error];
if (canDelete) {
[managedObjectContext deleteObject:myManagedObject];
}
else {
[self requestUserFeedback];
}
However, this method almost always returns NO.
Code example
Managed Object Model
Department
- Attribute: name
- Value: String
- Relationship: employees
- To Many
- Destination: Employee
- Delete Rule: Nullify
- Inverse: department
Employee
- Attribute: name
- Value: String
- Relationship: department
- To One
- Destination: Department
- Delete Rule: Nullify
- Inverse: employees
Code example:
NSManagedObject *department = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.managedObjectContext];
for (int i = 0; i < 10; i++) {
NSManagedObject *employee = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
[employee setValue:department forKey:@"department"];
if ([employee validateForDelete:NULL]) {
NSLog(@"Can delete employee");
}
else {
NSLog(@"WARNING: Cannot delete employee");
}
}
NSError *error;
BOOL canDelete = [department validateForDelete:&error];
if (canDelete) {
NSLog(@"Can delete department");
}
else {
NSLog(@"WARNING: Cannot delete department");
}
Output:
:
, canDelete - NO. , ? ()
Error Domain=NSCocoaErrorDomain
Code=1600
UserInfo= <>{
NSValidationErrorObject=< department object >
NSValidationErrorKey=employees
NSValidationErrorValue=Relationship 'employees' on managed object
}
, validateForDelete: , "", "". :
, , [sic] "deny" , .
NSManagedObjects , , .
, validateForDelete : , "deny" . "--".
Google Apple 2009 , : -[NSManagedObject validateForDelete:] - , , , .
? ? , :
, .
, , ?
- GitHub, , .