只有在没有关系的情况下才正确处理对象的删除?
我有一个 Person
实体,它属于一对多关系中的 Department
。
我希望能够在不再有与之关联的 Person
时删除 Department
(通过删除 Person
实体) ,或更改 Person
的 department
属性)。 ,我正在尝试使用以下处理程序来执行此操作:
- (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
实体执行提取。这是我应该做的事情吗?
I have a Person
entity which belongs to a Department
in a one to many relationship.
I would like to be able to delete the Department
when there are no more Person
s associated with it (either through the deletion of the Person
entity, or a change to the Person
's department
attribute). Right now, I'm trying to do so with the following handler for NSManagedObjectContextObjectsDidChangeNotification
(Currently just trying to see deletions, and delete appropriately):
- (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
}
}
}
}
However, the count of the number of people associated with the department doesn't change when I delete a person. I am not performing a fetch on the Department
entity. Is that something I should be doing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法:在您的部门实体类中实现
willSave
(您为每个实体提供了自己的类,对吗?*),让您的部门自行检查[self isDeleted] == NO 和
[[self person] count] == 0
,如果是,则删除自身。 (isDeleted
的检查不是可选的,因为在willSave
内更改自身的实体会触发对 willSave 的另一次调用。NSManagedObject willSave
的文档有更多信息。)这会推迟删除,直到上下文刷新回磁盘,这不应该是一个大问题。如果您确实需要部门在最后一个人离开后立即删除自身,请让您的部门实体使用 KVO 进行自我观察。在
awakeFromFetch
和awakeFromInsert
中将其注册为自己的“person”属性上的观察者,并在willTurnIntoFault
中取消注册。当 person 属性发生变化时,检查它是否为空。这需要更多工作,因此只有在第一种方法不适合您时才尝试。最后,请记住,Person->Department关系删除规则需要设置为“nullify”(当Person被删除时,它从部门的人员中删除),而不是“no action”(当Person被删除时,你负责自己清理部门)或“级联”(当任何人被删除时,其部门也被删除!)
(* MOGenerator 是维护每个实体类的一个非常好的助手。 http://rentzsch.github.com/mogenerator/)
Easiest way: implement
willSave
in your Department entity class (You are giving each entity its own class, right? *), have your department check itself for[self isDeleted] == NO
and[[self person] count] == 0
, and delete itself if so. (The check ofisDeleted
isn't optional, because an entity changing itself insidewillSave
triggers another call to willSave. The docs forNSManagedObject willSave
have more info.) This postpones the delete until the context is flushed back to disk, which shouldn't be a huge problem.If you do need the Department to delete itself the instant the last person leaves it, have your Department entity observe itself with KVO. Register it as an observer on its own "person" property in
awakeFromFetch
andawakeFromInsert
, and unregister inwillTurnIntoFault
. When the person property changes, check it for empty. This is more work, so only try it if the first way doesn't work for you.Finally, remember that the Person->Department relationship delete rule needs to be set to 'nullify' (when Person is deleted, it is removed from Department's persons), not 'no action' (when Person is deleted, you take responsibility for cleaning up the Department yourself) or 'cascade' (when any Person is deleted, its Department is deleted too!)
(* MOGenerator is a very nice helper for maintaining per-entity classes. http://rentzsch.github.com/mogenerator/ )
在您的
Person
子类中,重写prepareForDeletion
。如果部门
只剩下一个人
,则也删除该部门
。 Apple 的文档甚至建议使用prepareForDeletion
作为进行自定义删除传播的最佳位置。In your
Person
subclass, overrideprepareForDeletion
. If theDepartment
has only oneperson
left, delete theDepartment
too. Apple's documentation even suggests usingprepareForDeletion
as the best place to do custom delete propagation.