Custom methods for interacting with many users, not called when a delete rule is executed
I have a problem that I don't understand.
I have a simple basic data model with collection and cds. Each CD can be added to multiple Collections, so I added an intermediate object to make this possible. (edit: This is a simplification of my DataModel. An intermediate object is needed to nest collections in a collection)
So, when the CD is added to the collection, it looks like this: (IM is an intermediate object)
|==========| |==========| |==========|
| | | | | |
|Collection|=========| IM |=========| CD |
| | | | | |
|==========| |==========| |==========|
I have set up the delete rules so that if the CD is deleted by the user, the IM object is cascaded and the collection property is lost when the IM object is deleted. It behaves the way I wanted it to. There are no leftovers in the database.
But the strange thing is that if I override the access-to-many method to access the IM from the collection (which I assumed to be called), nothing happens.
I am using what Apple provides in their Master Data Programming Guide. link
For one object:
- (void)removeEmployeesObject:(Employee *)value
{
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"employees"
withSetMutation:NSKeyValueMinusSetMutation
usingObjects:changedObjects];
[[self primitiveEmployees] removeObject:value];
[self didChangeValueForKey:@"employees"
withSetMutation:NSKeyValueMinusSetMutation
usingObjects:changedObjects];
}
to remove multiple objects:
- (void)removeEmployees:(NSSet *)value
{
[self willChangeValueForKey:@"employees"
withSetMutation:NSKeyValueMinusSetMutation
usingObjects:value];
[[self primitiveEmployees] minusSet:value];
[self didChangeValueForKey:@"employees"
withSetMutation:NSKeyValueMinusSetMutation
usingObjects:value];
}
(taken from pdf, they use Employee as a many-to-relationship)
But these methods are not called when the delete rules are executed!
So my question is, the IM cascade object has been removed, but these methods are not being used. How can I detect this delete action that Core Data is doing?
Is there another way?
I want to know this because I want to set "dirtyFlag" for the whole collection that have changed, so I can update some things for those objects. (other properties, not explained here)
(A KVO on an object would be useless. I can't keep track of the full database, can I? Or should a Collection object have a KVO for this property?)
Hope someone can help me!
source to share
Alternatively, you can -prepareForDeletion
subclass IM managedObject. -prepareForDeletion
is automatically called by Core Data on the managed object just before the managedObjectContext is removed from it.
When called -prepareForDeletion
, all relations are still available and you can send a custom message -willDeleteIntermediateObject:
to all contained collections by passing self
as a parameter.
Then you can implement -[Collection willDeleteIntermediateObject:]
to check if it contains the last IM object, and if so, do so to remove managedObjectContext from it.
// IM.m
- (void)prepareForDeletion {
[super prepareForDeletion];
for (Collection *collection in self.collections) {
[collection willDeleteIntermediateObject:self];
}
}
// Collection.m
- (void)willDeleteIntermediateObject:(IM *)im {
if (self.intermediateObjects.count == 1 && [self.intermediateObjects.containsObject:im]) {
[self.managedObjectContext deleteObject:self];
}
}
source to share
Are you modifying your data with selected properties? In this case, the accessor methods for modifiable collections will not be called.
If you want to set a dirty flag for literally every deletion, this is perhaps the best template for a simple registration for notification NSManagedObjectContextObjectsDidChangeNotification
. Use the key NSDeletedObjectsKey
to remove deleted items from the dictionary userInfo
. You can put a dirty flag on your collections at this time.
source to share