I'm attempting to use NSManagedObject validateForDelete in order to provide timely feedback to delete actions by users. But the relationship delete rules don't seem to be getting applied as I would expect.

As a toy example I created a simple CoreData app with two entities, Department and Employee --employees and department are the respective inverse relationships.

The delete rule for Department employees to Employee department is Cascade. The delete rule for Employee department to Department employees is Nullify. I was expecting that if I had a single Department with a single Employee, then validateForDelete would return true on both of these managed objects. But it doesn't.

Can someone clear up my apparent misunderstanding, or suggest a better way of accomplishing my goal? In the context of this example, if Employee had a relationship to a third entity (say, a Stapler entity) with a delete rule of Deny, if the user attempted to delete the Employee's Department I would like to tell the user they must first delete the Employee's Stapler. If they did that, then I'd like deleting the Department to cascade to deleting the Employee.

Here's my test code:

NSManagedObject *department = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:moc];

NSError *error;
BOOL canDelete = [department validateForDelete:&error];
NSLog(@"1) For department canDelete = %d", canDelete);
if (! canDelete)
    NSLog(@"\terror = %@", [error localizedDescription]);


NSManagedObject *employee = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:moc];

canDelete = [employee validateForDelete:&error];
NSLog(@"2) For employee canDelete = %d", canDelete);
if (! canDelete)
    NSLog(@"\terror = %@", [error localizedDescription]);

[employee setDepartment:department];

canDelete = [department validateForDelete:&error];
NSLog(@"3) For department canDelete = %d", canDelete);
if (! canDelete)
    NSLog(@"\terror = %@", [error localizedDescription]);

canDelete = [employee validateForDelete:&error];
NSLog(@"4) For employee canDelete = %d", canDelete);
if (! canDelete)
    NSLog(@"\terror = %@", [error localizedDescription]);

And the resulting log output (removing the log statement data part for brevity):

1) For department canDelete = 1
2) For employee canDelete = 1
3) For department canDelete = 0
        error = Items cannot be deleted from employees.
4) For employee canDelete = 0
        error = Items cannot be deleted from department.


Thanks for any help,

Ward

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to