Skip to content

Commit

Permalink
Merge pull request #307 from green-code-initiative/rule/EC514-swift
Browse files Browse the repository at this point in the history
Added Swift version of EC514
  • Loading branch information
dedece35 authored May 30, 2024
2 parents 5b18cf5 + 4b80d4c commit 1077684
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
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()
}
}
----

0 comments on commit 1077684

Please sign in to comment.