Xiaoye220 / EmptyDataSet-Swift

🎄 DZNEmptyDataSet implement with Swift.A drop-in UITableView/UICollectionView superclass category for showing empty datasets whenever the view has no content to display. DZNEmptyDataSet with Swift.
MIT License
692 stars 99 forks source link

Delegate and Datasource should be weak #33

Closed ronakvora closed 5 years ago

ronakvora commented 5 years ago

Not having weak delegates can result in retain cycles that cause memory leaks.

extension UIScrollView : UIGestureRecognizerDelegate {

    public var emptyDataSetSource: EmptyDataSetSource?

    public var emptyDataSetDelegate: EmptyDataSetDelegate?

    public var isEmptyDataSetVisible: Bool { get }

    public func emptyDataSetView(_ closure: @escaping (EmptyDataSet_Swift.EmptyDataSetView) -> Void)

    public func reloadEmptyDataSet()
}

should be

extension UIScrollView : UIGestureRecognizerDelegate {

    public weak var emptyDataSetSource: EmptyDataSetSource?

    public weak var emptyDataSetDelegate: EmptyDataSetDelegate?

    public var isEmptyDataSetVisible: Bool { get }

    public func emptyDataSetView(_ closure: @escaping (EmptyDataSet_Swift.EmptyDataSetView) -> Void)

    public func reloadEmptyDataSet()
}
Xiaoye220 commented 5 years ago
objc_setAssociatedObject(self, &kEmptyDataSetSource, WeakObjectContainer(with: newValue), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
class WeakObjectContainer: NSObject {
    weak var weakObject: AnyObject?

    init(with weakObject: Any?) {
        super.init()
        self.weakObject = weakObject as AnyObject?
    }
}

EmptyDataSetSource and EmptyDataSetDelgate is a weakObject for WeakObjectContainer,because associatedObject does't have weak policy. So it will not cause memory leaks.

BeauNouvelle commented 5 years ago

Close this issue?

ronakvora commented 5 years ago

Yes

Sent from my iPhone

On Jul 29, 2019, at 5:52 PM, Beau Nouvelle notifications@github.com wrote:

Close this issue?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.