From 52c06cb87f4135320a2d8ee54b346ae53d77c01e Mon Sep 17 00:00:00 2001 From: Bug Magnet Date: Thu, 31 Aug 2023 11:20:13 +0200 Subject: [PATCH] Use the block based KVO API in AsyncOperation --- ios/Operations/AsyncOperation.swift | 36 ++++++----------------------- 1 file changed, 7 insertions(+), 29 deletions(-) diff --git a/ios/Operations/AsyncOperation.swift b/ios/Operations/AsyncOperation.swift index 7ae6eea6d2d5..ea8edb24c485 100644 --- a/ios/Operations/AsyncOperation.swift +++ b/ios/Operations/AsyncOperation.swift @@ -117,7 +117,7 @@ open class AsyncOperation: Operation { _error } - override public final var isReady: Bool { + dynamic override public final var isReady: Bool { stateLock.lock() defer { stateLock.unlock() } @@ -238,45 +238,23 @@ open class AsyncOperation: Operation { public let dispatchQueue: DispatchQueue + private var isReadyObserver: NSKeyValueObservation? public init(dispatchQueue: DispatchQueue? = nil) { self.dispatchQueue = dispatchQueue ?? DispatchQueue(label: "AsyncOperation.dispatchQueue") super.init() - addObserver( - self, - forKeyPath: #keyPath(isReady), - options: [], - context: &Self.observerContext - ) + isReadyObserver = observe(\.isReady, options: []) { operation, _ in + operation.checkReadiness() + } } deinit { - removeObserver(self, forKeyPath: #keyPath(isReady), context: &Self.observerContext) + // Clear the observer when the operation is deallocated to avoid leaking memory. + isReadyObserver = nil } // MARK: - KVO - private static var observerContext = 0 - - override public func observeValue( - forKeyPath keyPath: String?, - of object: Any?, - change: [NSKeyValueChangeKey: Any]?, - context: UnsafeMutableRawPointer? - ) { - if context == &Self.observerContext { - checkReadiness() - return - } - - super.observeValue( - forKeyPath: keyPath, - of: object, - change: change, - context: context - ) - } - @objc class func keyPathsForValuesAffectingIsReady() -> Set { [#keyPath(state)] }