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

Added Swift version of EC514 #307

Merged
merged 6 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- [#310] (https://github.com/green-code-initiative/ecoCode/issues/310) EC515 Swift port

- [#306](https://github.com/green-code-initiative/ecoCode/issues/306) Swift port of rule EC514
- [#315](https://github.com/green-code-initiative/ecoCode/pull/315) Add rule EC530 for javascript
- [#321](https://github.com/green-code-initiative/ecoCode/pull/321) Add rule EC522 for javascript (avoid brightness override)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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

[source,swift]
----
import CoreMotion

let motionManager = CMMotionManager()

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

== Compliant Code Example

[source,swift]
----
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()
}
}
----
Loading