Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ios: fix isLowerPowerModeEnabled deadlock crash #94

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 need to call `ProcessInfo.processInfo.isLowPowerModeEnabled` here
// more than once.
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())
}
}
Loading