核心数据:避免在多对多关系中的保留周期 [英] Core Data: avoiding retain cycles in to-many relationships

查看:155
本文介绍了核心数据:避免在多对多关系中的保留周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然在通过iOS开发学习我的方式,并与Core Data合作,刚刚遇到了保留周期。

I'm still learning my way through iOS development and working with Core Data and have just come across retain cycles.

这是我从理解核心数据编程指南,完成处理关系后,您可以使用托管对象上下文方法 refreshObject:mergeChanges 来确保保留周期被中断。

It is my understanding from reading the Core Data Programming Guide that after you're done working with a relationship, you use the managed object context method refreshObject:mergeChanges to ensure that the retain cycle is broken.

所以我们说我在部门和员工之间有一对多的关系,在我的代码中,我从部门访问员工关系,这意味着我现在需要循环每个员工对象并调用 refreshObject:mergeChanges 方法?在代码中这将是

So lets say I have a to-many relationship between a Department and its Employees, and in my code I access the employees relationship from department, does that mean I'll now need to loop through each employee object and call refreshObject:mergeChanges method? In code this would be

for (Employee *anEmployee in department.employees) {
  //some code that accesses an employee's properties

  [context refreshObject:enEmployee mergeChanges:NO];
}

似乎如果我不这样做,现在将包含对部门的引用,我将以保留周期结束。

It seems that if I don't do that, each employee object I access will now contain a reference to the department and I will end up with retain cycles.

我的理解是否正确?这是在处理Core Data中的多对多关系时的标准方法吗?感谢。

Is my understanding correct here? Is this a standard approach when dealing with to-many relationships in Core Data? Thanks.

推荐答案

您可以查看破解关系保留循环,保留循环对于防止释放不需要的对象是必要的。

As you can check at Breaking Relationship Retain Cycles, the retain cycles are necessary to prevent deallocation of unwanted objects. It means that you keep the the object retained while you are using it.

应该使用 refreshObject:mergeChanges 如果您完成了该对象,并且想要将其变为故障,尽可能处理内存。它不一定会释放对象在关系的另一端,它将只设置一个标志的核心数据,如果必要的对象可以变成故障。

The refreshObject:mergeChanges should be used if you are done with that object and you want to turn it into fault, to dispose memory if possible. It won't necessarily release the object in the other end of the relationship, it will only set a flag to core data that the object can be turned into fault if necessary.

这篇关于核心数据:避免在多对多关系中的保留周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆