Digital PCT265 story ONEAPP-7249 - Fix notification retain cycle.

This commit is contained in:
Hedden, Kyle Matthew 2024-05-08 17:49:33 -04:00
parent 658850400f
commit 067f63e6de
3 changed files with 25 additions and 4 deletions

View File

@ -65,7 +65,9 @@ import MVMCore
(pagesToListenFor()?.count ?? 0 > 0 || modulesToListenFor()?.count ?? 0 > 0)
else { return }
observingForResponses = NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: NotificationResponseLoaded), object: nil, queue: pageUpdateQueue, using: responseJSONUpdated(notification:))
observingForResponses = NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: NotificationResponseLoaded), object: nil, queue: pageUpdateQueue) { [weak self] notification in
self?.responseJSONUpdated(notification: notification)
}
}
open func stopObservingForResponseJSONUpdates() {

View File

@ -137,6 +137,7 @@ public class PollingBehavior: NSObject, PageVisibilityBehavior, PageMoleculeTran
}
deinit {
debugLog("deinit")
pollTimer?.cancel()
}
}

View File

@ -15,7 +15,16 @@ public class ReplaceableMoleculeBehaviorModel: PageBehaviorModelProtocol {
public var moleculeIds: [String]
}
public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior {
public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior, CoreLogging {
public var loggingPrefix: String {
"\(self) \(ObjectIdentifier(self))\n\(moleculeIds)\n"
}
public static var loggingCategory: String? {
String(describing: Self.self)
}
var moleculeIds: [String]
var modulesToListenFor: [String]
private var observingForResponses: NSObjectProtocol?
@ -34,9 +43,11 @@ public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior {
self.delegateObject = delegateObject
guard let pageType = delegateObject?.moleculeDelegate?.getTemplateModel()?.pageType else { return }
MVMCoreViewControllerMappingObject.shared()?.addOptionalModules(toMapping: moleculeIds, forPageType: pageType)
Self.debugLog("Initializing for \((model as! ReplaceableMoleculeBehaviorModel).moleculeIds)")
}
public func onPageNew(rootMolecules: [MoleculeModelProtocol], _ delegateObject: MVMCoreUIDelegateObject?) {
debugLog("onPageNew")
self.delegateObject = delegateObject
let shouldListenForListUpdates = delegateObject?.moleculeListDelegate != nil
if shouldListenForListUpdates {
@ -70,7 +81,9 @@ public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior {
let pageUpdateQueue = OperationQueue()
pageUpdateQueue.maxConcurrentOperationCount = 1
pageUpdateQueue.qualityOfService = .userInteractive
observingForResponses = NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: NotificationResponseLoaded), object: nil, queue: pageUpdateQueue, using: responseJSONUpdated(notification:))
observingForResponses = NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: NotificationResponseLoaded), object: nil, queue: pageUpdateQueue) { [weak self] notification in
self?.responseJSONUpdated(notification: notification)
}
}
private func stopListeningForModuleUpdates() {
@ -97,7 +110,7 @@ public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior {
guard modules.count > 0 else { return }
#if LOGGING
let requestParams = (notification.userInfo?["MVMCoreLoadObject"] as? MVMCoreLoadObject)?.requestParameters
MVMCoreLoggingHandler.shared()?.handleDebugMessage("Replacing \(modules.map { $0.id }) from \(requestParams?.url?.absoluteString ?? "unknown"), e2eId: \(requestParams?.identifier ?? "unknown")")
debugLog("Replacing \(modules.map { $0.id }) from \(requestParams?.url?.absoluteString ?? "unknown"), e2eId: \(requestParams?.identifier ?? "unknown")")
#endif
delegateObject?.moleculeDelegate?.replaceMoleculeData(modules) { replacedModels in
let modules = replacedModels.compactMap { modulesLoaded.dictionaryForKey($0.id) }
@ -113,4 +126,9 @@ public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior {
}
return try modelType.decode(jsonDict: moduleJSON as [String : Any]) as! MoleculeModelProtocol
}
deinit {
debugLog("deinit")
stopListeningForModuleUpdates()
}
}