From 1c77b4a9865f6376b38fba389a5d658d48790e44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Augustyniak?= Date: Fri, 1 Nov 2024 15:05:05 -0400 Subject: [PATCH] ios: fix isLowerPowerModeEnabled deadlock crash (#94) --- .../providers/LowPowerStateProvider.swift | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/platform/swift/source/default/events/resource/providers/LowPowerStateProvider.swift b/platform/swift/source/default/events/resource/providers/LowPowerStateProvider.swift index 1b4f10fb..a52ecf44 100644 --- a/platform/swift/source/default/events/resource/providers/LowPowerStateProvider.swift +++ b/platform/swift/source/default/events/resource/providers/LowPowerStateProvider.swift @@ -9,11 +9,36 @@ import Foundation import UIKit final class LowPowerStateProvider { - private let processInfo = ProcessInfo.processInfo + private let isLowerPowerModeEnabled = Atomic(ProcessInfo.processInfo.isLowPowerModeEnabled) + + init() { + // Accessing `ProcessInfo.processInfo.isLowPowerModeEnabled` frequently causes occasional crashes + // on iOS 15 (up to at least version 15.2). To reduce these calls, subscribe to + // `NSProcessInfoPowerStateDidChange` notifications and track the state of `isLowPowerModeEnabled` + // locally. This minimizes the number of `ProcessInfo.processInfo.isLowPowerModeEnabled` calls here + // to one. + NotificationCenter + .default + .addObserver( + self, + selector: #selector(powerStateDidChange(_:)), + name: Notification.Name.NSProcessInfoPowerStateDidChange, + object: nil + ) + } + + @objc + private func powerStateDidChange(_ userInfo: Any) { + self.isLowerPowerModeEnabled.update { $0 = ProcessInfo.processInfo.isLowPowerModeEnabled } + } + + deinit { + NotificationCenter.default.removeObserver(self) + } } extension LowPowerStateProvider: ResourceSnapshotProvider { func makeSnapshot() -> ResourceSnapshot? { - return LowPowerStateSnapshot(lowPowerModeEnabled: self.processInfo.isLowPowerModeEnabled) + return LowPowerStateSnapshot(lowPowerModeEnabled: self.isLowerPowerModeEnabled.load()) } }