green-code-initiative / ecoCode

Reduce the environmental footprint of your software programs with SonarQube
https://ecocode.io
GNU General Public License v3.0
139 stars 76 forks source link

[EC514] Swift port #306

Closed zippy1978 closed 3 months ago

zippy1978 commented 3 months ago

Most iOS devices have built-in sensors that measure motion, orientation, and various environmental conditions. Additionally, they have image sensors (a.k.a. Camera) and geo-positioning sensors (a.k.a. GPS).

The common point of all these sensors is that they consume significant power while in use. Their common issue is processing data unnecessarily when the app is in an idle state, typically when it enters the background or becomes inactive.

Consequently, calls to start and stop sensor updates must be carefully managed for motion sensor: CMMotionManager#startAccelerometerUpdates()/CMMotionManager#stopAccelerometerUpdates().
Failing to do so can drain the battery quickly.

Noncompliant Code Example

import CoreMotion

let motionManager = CMMotionManager()

func startMotionUpdates() {
    if motionManager.isAccelerometerAvailable {
        motionManager.startAccelerometerUpdates(to: .main) { data, error in
            // Handle accelerometer updates
        }
    }
}

Compliant Code Example

import CoreMotion

let motionManager = CMMotionManager()

func startMotionUpdates() {
    if motionManager.isAccelerometerAvailable {
        motionManager.startAccelerometerUpdates(to: .main) { data, error in
            // Handle accelerometer updates
        }
    }
}

func stopMotionUpdates() {
    if motionManager.isAccelerometerActive {
        motionManager.stopAccelerometerUpdates()
    }
}