delete managed object when to-many relationship reaches zero
delete managed object when to-many relationship reaches zero
- Subject: delete managed object when to-many relationship reaches zero
- From: Jesse Grosjean <email@hidden>
- Date: Tue, 14 Feb 2006 08:47:08 -0500
I have three managed objects:
Entry
- entryData, to-one relationship to EntryData (NSNullifyDeleteRule)
EntryData
- entries, to-many relationship to Entry (NSNullifyDeleteRule)
- blob, to-one relationship to Blob (NSCascadeDeleteRule)
Blob
- entryData, to-one relationship to EntryData (NSNullifyDeleteRule)
My goal is to add some extra logic code so that EntryDatas will be
deleted when the number of entries in their to-many "entries"
relationship reaches zero. So it should work like garbage collection,
when no more Entries reference the EntryData the EntryData should
disappear. To do this I added this code to my EntryData class:
- (void)removeEntriesObject:(NSManagedObject *)value {
...
if ([[self valueForKey:@"entries"] count] == 0) {
[[self managedObjectContext] deleteObject:self];
}
}
This almost works, the EntryData is deleted when the last Entry in
its entries relationship is removed, but this delete doesn't seem to
be completely processed by the managed object context, the Blob
referred to by the EntryData in this case is not deleted even though
that relationship has a NSCascadeDeleteRule. The NSCascadeDeleteRule
does work properly if I replace the above code with this code so that
the delete of the EntryData doesn't happen until the next pass
through the event loop.
- (void)removeEntriesObject:(NSManagedObject *)value {
...
if ([[self valueForKey:@"entries"] count] == 0) {
[[self managedObjectContext] performSelector:@selector
(deleteObject:) withObject:self afterDelay:0];
}
}
My guess is that this is happening because the removeEntriesObject
method is called while the managed object context is already
processing changes and maybe it doesn't handle the case properly when
objects are deleted as it processes changes. I'm not sure if that's a
bug or not, I'll report it if someone tells me that it is a bug.
Ideally I don't want to use the
performSelector:withObject:afterDelay: hack, but I do want the delete
rules in my EntryData to be respected. Can anyone suggest the best
way to solve this problem? How to automatically delete an object in
the managed object context when the number of objects referring to it
reaches zero?
I've put up an example application here:
http://www.hogbaysoftware.com/files/
DeleteWhenRelationshipReachesZero.zip
To test it:
1. Click "Add Entries" to add an Entry, EntryData, and Blob
2. Click "Delete Entries" to delete the Entry (Expecting the
EntryData and Blob to also be deleted because of logic in code and
delete rules)
3. Click "List Objects" and see that Blob wasn't ever deleted
Jesse
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden