Creating an instance of ProcessInfo messes up low power mode results

When I create a ProcessInfo instance using ProcessInfo(), it seems to mess up the internally created singleton instance. Because afterwards, when I use ProcessInfo.processInfo to retrieve the low power mode state of the device, it returns the same result (the mode when ProcessInfo() was called) regardless of what the actual mode is at that moment.

I tested this on 15.6.1.

I have the same issue too. It doesn't happen on all devices but is consistent on iPhone 8 with iOS 16.7.5. The NSProcessInfoPowerStateDidChange is never called and the state is never updated during the same app session, it keeps returning the same value. Most weird the systemUptimeChanges, isLowPowerModeEnabled is the only state that never changes. Tried to dispatch on main thread, but the result are the same.

protocol SystemPowerInfoProvider {
    var isLowPowerModeEnabled: AnyPublisher<Bool, Never> { get }
}

final class DefaultSystemBatteryOptimizationStateProvider: SystemPowerInfoProvider {
    var isLowPowerModeEnabled: AnyPublisher<Bool, Never> {
        _isLowPowerModeEnabled
    }
    
    private lazy var _isLowPowerModeEnabled: AnyPublisher<Bool, Never> =  {
        
        return Just(ProcessInfo.processInfo.isLowPowerModeEnabled)
            .merge(with: NotificationCenter.default
                .publisher(for: Notification.Name.NSProcessInfoPowerStateDidChange)
                .map { notification -> Bool? in
                    guard let processInfo = notification.object as? ProcessInfo else {
                        return nil
                    }
                    return processInfo.isLowPowerModeEnabled
                }
                .compactMap { $0 }
            )
            .eraseToAnyPublisher()
    }()

}

Where you able to solve it?

I avoid creating an instance of ProcessInfo. I use the singleton instead. So I don't have this problem anymore. I reported it because it seemed like that buggy behaviour.

Creating an instance of ProcessInfo messes up low power mode results
 
 
Q