私は1対多の関係Personに属するエンティティを持っていDepartmentます。
関連付けられてDepartmentいるがなくなったときに削除できるようにしたいと思います(エンティティの削除または's属性の変更のいずれかによって)。現在、私は次のハンドラーを使用してこれを実行しようとしています(現在、削除を確認し、適切に削除しようとしています)。PersonPersonPersondepartmentNSManagedObjectContextObjectsDidChangeNotification
- (void)managedObjectDidChange:(NSNotification *)notification {
NSSet *updatedObjects = [[notification userInfo] objectForKey:NSDeletedObjectsKey];
for (NSManagedObject *obj in updatedObjects) {
if ([obj.entity.name isEqualToString:@"Person"]) {
NSLog(@"Person Changed");
NSManagedObject *department = [(Person *)obj department];
NSLog(@"%i", [[department valueForKey:@"person"] count]);
if ([[department] valueForKey:@"person"] count] == 0) {
NSLog(@"Department has no more people associated with it");
// deletion code
}
}
}
}
ただし、人を削除しても、所属部署の人数は変わりません。Departmentエンティティに対してフェッチを実行していません。それは私がすべきことですか?