objective c - Core Data prepareForDeletion woes -
i'm interested how preparefordeletion works.
i have pretty simple object graph. 1 class called individual
, 1 class called group
. relationship between them many-to-many.
what have done far when delete group, check if individuals belong group belong group being deleted, if delete individuals well.
it looks this:
group.m
- (void)preparefordeletion { //individual check: if group last associated individual, delete individual (individual *individual in self.individuals) { if ([individual.groups count] == 1) { [self.managedobjectcontext deleteobject:individual]; } } }
now had idea same approach individual
. is, when individual
deleted check if individual
deleted last 1 in group. if it's last individual
group should deleted well.
individual.m
- (void)preparefordeletion { // group check: if individual last associated group, delete group (group *group in self.groups) { if ([group.individuals count] == 1) { [self.managedobjectcontext deleteobject:group]; } } }
so if choose route delete individual has has 1 relationship 1 group. both individual , group should deleted:
individual *individualtodelete = ... [moc deleteobject:individualtodelete];
now in preparefordeletion in individual.m delete group (since has 1 individual) go preparefordeletion in group.m , there try delete group well? since has 1 individual?
to remedy should call save after calling delete, like:
- (void)preparefordeletion { // group check: if individual last associated group, delete group (group *group in self.groups) { if ([group.individuals count] == 1) { [self.managedobjectcontext deleteobject:group]; [self.managedobjectcontext save:nil]; } } }
Comments
Post a Comment