Skip to content

Commit b8f542b

Browse files
committed
Use weak reference for analytics instance to avoid retain cycles and break cycle between ConsentManager and Provider
1 parent 99d3ef6 commit b8f542b

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

Sources/SegmentConsent/Blocker.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ConsentBlocker: EventPlugin {
1414
internal let store: Store
1515

1616
public var type: PluginType = .before
17-
public var analytics: Segment.Analytics?
17+
public weak var analytics: Segment.Analytics?
1818

1919
public init(destinationKey: String, store: Store) {
2020
self.destinationKey = destinationKey

Sources/SegmentConsent/Manager.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Sovran
1111

1212
public class ConsentManager: EventPlugin {
1313
public let type: PluginType = .before
14-
public var analytics: Analytics? = nil
14+
public weak var analytics: Analytics? = nil
1515
public let store = Store()
1616

1717
internal var provider: ConsentCategoryProvider
@@ -23,7 +23,10 @@ public class ConsentManager: EventPlugin {
2323
self.provider = provider
2424
self.consentChange = consentChanged
2525

26-
self.provider.setChangeCallback(notifyConsentChanged)
26+
// call notifyConsentChanged from a closure to avoid retain cycles.
27+
self.provider.setChangeCallback( { [weak self] in
28+
self?.notifyConsentChanged()
29+
})
2730
}
2831

2932
public func configure(analytics: Analytics) {

0 commit comments

Comments
 (0)