-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #307 from green-code-initiative/rule/EC514-swift
Added Swift version of EC514
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
---- |