官术网_书友最值得收藏!

How it works...

We have started our example by creating two classes, Person and Dog. The Person class has one-to-many relation to the Dog class via the property array dogs. The Dog class has one-to-one relation to class Person via the property owner. Everything looks good, and it works fine if you tested, but unfortunately we did a terrible mistake. We have a retain cycle problem here, which means we have two objects in memory; each one has a strong reference to the other. This leads to a cycle that prevents both of them from being deallocated from memory.

This problem is a common problem in iOS, and not all developers note it while coding. We call it as parent-child relation. Parent (in our case, it's the Person class) should always have a strong reference to child (the Dog class); child should always have a weak reference to the parent. Child doesn't need to have strong reference to parent, as child should never exit when parent is deallocated from memory.

To solve such a problem, you have to break the cycle by marking one of these references as weak. In step 2, we see how we solved the problem by marking the property owner as weak.

主站蜘蛛池模板: 石楼县| 江永县| 大安市| 雷山县| 柞水县| 禄丰县| 扎鲁特旗| 栾川县| 资源县| 黔西县| 普洱| 响水县| 德江县| 宣恩县| 枣庄市| 邻水| 万盛区| 来宾市| 新泰市| 柳江县| 孝感市| 盱眙县| 襄樊市| 宿松县| 安顺市| 民权县| 手游| 神农架林区| 华宁县| 阿拉善左旗| 和平区| 莱西市| 勐海县| 苏尼特右旗| 湖南省| 宁远县| 望谟县| 山阴县| 青州市| 新乡市| 荣昌县|