管理 NSManagedObject

发布于 2024-12-02 06:38:49 字数 929 浏览 0 评论 0原文

如果我有一个 NSManagedObject,它维护一组另一种类型的 NSManagedObject 作为其属性之一,并且我将该集合设置为 nil 或将其替换为不同的集合,那么原始集合中的托管对象会发生什么情况?它们会成为数据库中的孤儿吗?我知道如果您删除父托管对象,您可以级联删除,但这不是同一件事。

我想无论是不是一套都没关系。如果一个托管对象引用另一个托管对象,现在您希望它引用不同的对象并且不再关心旧对象,是否必须删除它。

我自己做了一些测试:

NSSet *set = [managedObject getDefaultSet]; //at this point I can access the set
[managedObject setDefaultSet:newSet]; //at this point I can't access it anymore!

似乎 CoreData 做了一些魔法,因为在我用新集替换该集后,我无法再使用 gdb 访问旧集。但这是否意味着它不再存在于数据库中?我无法访问它,但我是否还需要将其从数据库中物理删除?

编辑:新测试:

NSSet *set = [managedObject getDefaultSet]; //at this point I can access the set
id object = [set anyObject];
[managedObject setDefaultSet:newSet]; //at this point I can't access it anymore! BUT I can still access the object

我已经验证该对象仍然存在,所以现在我想我确实需要删除它。我的猜测是覆盖 setDefaultSet 属性。核心数据的默认变异器属性是什么样的?我认为我不想重写 setValue:forKey 特别是因为类引用明确告诉我不要这样做。

If I have an NSManagedObject which maintains as one of its properties a set of another type of NSManagedObject, and I set that set to nil or replace it with a different set, what happens to the managed objects in the original set? Will they be orphaned in the database? I know if you delete the parent managed object you can cascade deletes but this isn't the same thing.

I suppose it doesn't matter if it's a set or not. If one managed object references another and now you want it to reference something different and no longer care about the old object, do you have to delete it.

I did some of my own testing:

NSSet *set = [managedObject getDefaultSet]; //at this point I can access the set
[managedObject setDefaultSet:newSet]; //at this point I can't access it anymore!

It seems that CoreData did some magic as after I replace the set with a new set, I can no longer access the old set using gdb. BUT does that mean that it's no longer in the database? I can't access it but do I also need to physically remove it from the DB?

EDIT: New test:

NSSet *set = [managedObject getDefaultSet]; //at this point I can access the set
id object = [set anyObject];
[managedObject setDefaultSet:newSet]; //at this point I can't access it anymore! BUT I can still access the object

I've verified that the object is still in existence so now I guess I do need to delete it. My guess would be to override the setDefaultSet property. What does a default mutator property look like for core data? I don't think I want to override setValue:forKey especially since the class reference tells me explicitly not to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

中二柚 2024-12-09 06:38:49

这是我现在要使用的解决方案:

- (void)setDefaultSet:(NSSet *)set
{
   NSMutableSet *oldSet = [self mutableSetValueForKey:@"defaultSet"];
   for (NSManagedObject *object in oldSet)
       //remove object from context
       [oldSet removeAllObjects];
   [oldSet addObjectsFromArray:[set allObjects]];
}

我添加了自己的访问器/修改器

Here's the solution I'm going with for now:

- (void)setDefaultSet:(NSSet *)set
{
   NSMutableSet *oldSet = [self mutableSetValueForKey:@"defaultSet"];
   for (NSManagedObject *object in oldSet)
       //remove object from context
       [oldSet removeAllObjects];
   [oldSet addObjectsFromArray:[set allObjects]];
}

I added my own accessor/mutator

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文