Reputation: 1043
I am having a many to many relationship between two tables say table A and B.
For the sake of business logic I have put this property with 'No Action' as deletion rule.
step 1: Now at a point I am deleting table A items.
step 2: After deletion of tableA items I want to delete those items in Table B which are not connected with any item in table A.
so the code snippet for deletion for step 2 is somewhat like this:
for (TableBItem* item in fetchedObjects) {
if ([[item.tableAproperty allObjects] count] == 0) {
[context deleteObject:item];
}
}
//save context
[context save:&error]
After this when i am fetching tableB objects it shows me even those items which i tried to delete.
If I kill the application and launch again then the fetching items works as expected.
Any idea what might be going wrong here. Thanks in advance.
Upvotes: 1
Views: 372
Reputation: 80273
A proven method to accomplish this is to override the remove object Xcode-generated setter method in your managed object subclass for TableBItem
(a terrible name BTW - also, tableAProperty
is misleading as it is a relationship and not an attribute).
-(void)removeTableAPropertyObject:(TableAItem *)value {
[super removeTableAPropertyObject:value];
if (!self.tableAProperty.count) {
[self.managedObjectContext deleteObject:self];
}
}
Upvotes: 1
Reputation: 3763
One thing that comes to mind is to set the delete rule to 'nullfiy'. This way, deleting an entity A will nullify the relation from B to A. Hence, no B will be linked to A after deleting all A's. I can't really give you an explanation about the difference after re-launch, I guess some staleness or cache/faulting effects.
Upvotes: 0