Cascade Delete won't delete Department in Apple Sample Code
Cascade Delete won't delete Department in Apple Sample Code
- Subject: Cascade Delete won't delete Department in Apple Sample Code
- From: Jerry Krinock <email@hidden>
- Date: Mon, 22 Jun 2009 13:55:24 -0700
Core Data documentation [1] describes the Cascade Delete Rule as
follows:
"Delete the objects at the destination of the relationship. For
example, if you delete a department, fire all the employees in that
department at the same time."
In order to understand another problem, I tried this on this the
inverse relationship in Apple's DepartmentAndEmployees Sample Code
project. That is, in the data model, I selected the Employee -->
Department to-one relationship by selecting the 'department'
relationship in the 'Employee' entity, changed its Delete Rule to
Cascade, added some code to log and -processPendingChanges to the
MyDocument class [2], built, and run.
Running, in a new document, I added an Employee, then deleted it. I
expected that the Department would be deleted also, but it was not.
Now I understand that deleting the Department might wreak havoc in
this particular project, or generate a 'deny' error, but according to
the console log [3] it didn't even try. Can anyone suggest why not?
Jerry
[1] http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html#/
/apple_ref/doc/uid/TP40001857-SW1
[2]
In -initWithType:error:, I added this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mocModelChanged:)
name:NSManagedObjectContextObjectsDidChangeNotification
object:[self
managedObjectContext]] ;
And then I added this method:
- (void)mocModelChanged:(NSNotification*)notification {
NSSet* insertedObjects = [[notification userInfo]
objectForKey:NSInsertedObjectsKey] ;
NSSet* deletedObjects = [[notification userInfo]
objectForKey:NSDeletedObjectsKey] ;
NSSet* updatedObjects = [[notification userInfo]
objectForKey:NSUpdatedObjectsKey] ;
NSLog(@"******** Model Changed!! *********") ;
NSManagedObject* object ;
for (object in insertedObjects) {
NSLog(@">>> inserted object: %@", object) ;
}
for (object in deletedObjects) {
NSLog(@"<<< deletedObject: %@", object) ;
}
for (object in updatedObjects) {
NSLog(@"!!! updatedObject: %@ %p", [[object entity] name],
object) ;
NSLog(@" !!! changedValues: %@", [object changedValues]) ;
}
NSLog(@"****** End of Model Changes ******\n") ;
/*DB?Line*/ NSLog(@"4550: Processing...") ;
[[self managedObjectContext] processPendingChanges] ;
}
[3] Console log output when upon deleting 1 of 1 employee:
2009-06-22 13:47:11.380 DepartmentAndEmployees[82461:10b] ********
Model Changed!! *********
2009-06-22 13:47:11.381 DepartmentAndEmployees[82461:10b] <<<
deletedObject: <Employee: 0x1c54b0> (entity: Employee; id: 0x1b9760 <x-coredata:///Employee/tB41AABC2-E997-4739-B199-8EFB3B7987AE6
> ; data: {
department = nil;
directReports = (
);
employeeID = 3;
firstName = First;
lastName = Last;
manager = nil;
salary = 0;
})
2009-06-22 13:47:11.382 DepartmentAndEmployees[82461:10b] !!!
updatedObject: Department 0x164ef0
2009-06-22 13:47:11.383 DepartmentAndEmployees[82461:10b] !!!
changedValues: {
}
2009-06-22 13:47:11.383 DepartmentAndEmployees[82461:10b] ****** End
of Model Changes ******
2009-06-22 13:47:11.384 DepartmentAndEmployees[82461:10b] 4550:
Processing...
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden