Zack Zhu Zack Zhu - 3 months ago 11
iOS Question

Is there a retain cycle between UICollectionView and it's collectionViewLayout?

According to class references of UICollectionView and UICollectionViewLayout, UICollectionView holds a strong reference to its collectionViewLayout property, and UICollectionViewLayout also holds a strong reference to its collectionView property.

In UICollectionView,

@property(nonatomic, strong) UICollectionViewLayout *collectionViewLayout


In UICollectionViewLayout, (strong is a default property attribute).

@property(nonatomic, readonly) UICollectionView *collectionView


Looks like a retain cycle here. Why doesn't UICollectionViewLayout hold a weak reference to collectionView instead?

Answer

In UICollectionViewLayout, (strong is a default property attribute).

@property(nonatomic, readonly) UICollectionView *collectionView

It is the default but a readonly property has no storage, it is just a getter. Internally the layout must have a weak reference to the collection view. I don't have any knowledge of its actual source code but it could look like this:

@interface UICollectionViewLayout ()
@property(nonatomic, weak, readwrite) UICollectionView *collectionView;
@end

But if you ran Instruments and actually found a leak you should filed a radar because it is definitely a bug in my book.

Comments