From b8f542bb752784876fc8f6ddcd5fd81933ddc00a Mon Sep 17 00:00:00 2001 From: Romain Rolland Date: Tue, 22 Jul 2025 16:39:11 +0200 Subject: [PATCH] Use weak reference for analytics instance to avoid retain cycles and break cycle between ConsentManager and Provider --- Sources/SegmentConsent/Blocker.swift | 2 +- Sources/SegmentConsent/Manager.swift | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Sources/SegmentConsent/Blocker.swift b/Sources/SegmentConsent/Blocker.swift index f1444e6..5e80afa 100644 --- a/Sources/SegmentConsent/Blocker.swift +++ b/Sources/SegmentConsent/Blocker.swift @@ -14,7 +14,7 @@ public class ConsentBlocker: EventPlugin { internal let store: Store public var type: PluginType = .before - public var analytics: Segment.Analytics? + public weak var analytics: Segment.Analytics? public init(destinationKey: String, store: Store) { self.destinationKey = destinationKey diff --git a/Sources/SegmentConsent/Manager.swift b/Sources/SegmentConsent/Manager.swift index f3cbf12..edc8bd2 100644 --- a/Sources/SegmentConsent/Manager.swift +++ b/Sources/SegmentConsent/Manager.swift @@ -11,7 +11,7 @@ import Sovran public class ConsentManager: EventPlugin { public let type: PluginType = .before - public var analytics: Analytics? = nil + public weak var analytics: Analytics? = nil public let store = Store() internal var provider: ConsentCategoryProvider @@ -23,7 +23,10 @@ public class ConsentManager: EventPlugin { self.provider = provider self.consentChange = consentChanged - self.provider.setChangeCallback(notifyConsentChanged) + // call notifyConsentChanged from a closure to avoid retain cycles. + self.provider.setChangeCallback( { [weak self] in + self?.notifyConsentChanged() + }) } public func configure(analytics: Analytics) {