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

[EC514] Swift port #306

Closed
zippy1978 opened this issue May 29, 2024 · 0 comments · Fixed by #307
Closed

[EC514] Swift port #306

zippy1978 opened this issue May 29, 2024 · 0 comments · Fixed by #307
Assignees
Labels
🗃️ rule rule improvment or rule development or bug

Comments

@zippy1978
Copy link
Contributor

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()
    }
}
@zippy1978 zippy1978 added the 🗃️ rule rule improvment or rule development or bug label May 29, 2024
@zippy1978 zippy1978 self-assigned this May 29, 2024
@dedece35 dedece35 linked a pull request May 30, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗃️ rule rule improvment or rule development or bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant