只有在没有关系的情况下才正确处理对象的删除?

发布于 2024-10-17 04:28:54 字数 998 浏览 6 评论 0原文

我有一个 Person 实体,它属于一对多关系中的 Department

我希望能够在不再有与之关联的 Person 时删除 Department(通过删除 Person 实体) ,或更改 Persondepartment 属性)。 ,我正在尝试使用以下处理程序来执行此操作:

- (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 Persons 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洋洋洒洒 2024-10-24 04:28:54

最简单的方法:在您的部门实体类中实现 willSave (您为每个实体提供了自己的类,对吗?*),让您的部门自行检查 [self isDeleted] == NO 和 [[self person] count] == 0,如果是,则删除自身。 (isDeleted 的检查不是可选的,因为在 willSave 内更改自身的实体会触发对 willSave 的另一次调用。NSManagedObject willSave 的文档有更多信息。)这会推迟删除,直到上下文刷新回磁盘,这不应该是一个大问题。

如果您确实需要部门在最后一个人离开后立即删除自身,请让您的部门实体使用 KVO 进行自我观察。在 awakeFromFetchawakeFromInsert 中将其注册为自己的“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 of isDeleted isn't optional, because an entity changing itself inside willSave triggers another call to willSave. The docs for NSManagedObject 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 and awakeFromInsert, and unregister in willTurnIntoFault. 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/ )

述情 2024-10-24 04:28:54

在您的 Person 子类中,重写 prepareForDeletion。如果部门只剩下一个,则也删除该部门。 Apple 的文档甚至建议使用 prepareForDeletion 作为进行自定义删除传播的最佳位置。

- (void)prepareForDeletion
{
    [super prepareForDeletion];

    if (self.department.persons.count == 1) {
        [self.managedObjectContext deleteObject:self.department];
    }
}

In your Person subclass, override prepareForDeletion. If the Department has only one person left, delete the Department too. Apple's documentation even suggests using prepareForDeletion as the best place to do custom delete propagation.

- (void)prepareForDeletion
{
    [super prepareForDeletion];

    if (self.department.persons.count == 1) {
        [self.managedObjectContext deleteObject:self.department];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文