patternswiftMinor
Core location background update and upload to server
Viewed 0 times
updatebackgroundcoreserveruploadandlocation
Problem
I want to upload a user's location to a server to send them a location-sensitive alert, so the location needs to be fairly accurate:
```
import Foundation
import CoreLocation
protocol LocationServiceDelegate {
func tracingLocation(currentLocation: CLLocation)
func tracingLocationDidFailWithError(error: NSError)
}
class LocationService: NSObject, CLLocationManagerDelegate {
class var sharedInstance: LocationService {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: LocationService? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = LocationService()
}
return Static.instance!
}
var locationManager: CLLocationManager?
var lastLocation: CLLocation?
var delegate: LocationServiceDelegate?
override init() {
super.init()
self.locationManager = CLLocationManager()
guard let locationManager = self.locationManager else {
return
}
if #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates = true
} else {
// Fallback on earlier versions
};
if CLLocationManager.authorizationStatus() == .NotDetermined {
// you have 2 choice
// 1. requestAlwaysAuthorization
// 2. requestWhenInUseAuthorization
locationManager.requestAlwaysAuthorization()
}
locationManager.desiredAccuracy = kCLLocationAccuracyBest // The accuracy of the location data
locationManager.distanceFilter = 2000 // The minimum distance (measured in meters) a device must move horizontally before an update event is generated.
locationManager.delegate = self
}
func startUpdatingLocation() {
print("Starting Location Updates")
self.locationManager?.startUpdatingLocation()
}
func stopUpdatingLocation() {
print("Stop Location Updates")
self.locationManager?.stopUpdatingLocation()
}
// CLLocationManagerDelegate
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [C
```
import Foundation
import CoreLocation
protocol LocationServiceDelegate {
func tracingLocation(currentLocation: CLLocation)
func tracingLocationDidFailWithError(error: NSError)
}
class LocationService: NSObject, CLLocationManagerDelegate {
class var sharedInstance: LocationService {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: LocationService? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = LocationService()
}
return Static.instance!
}
var locationManager: CLLocationManager?
var lastLocation: CLLocation?
var delegate: LocationServiceDelegate?
override init() {
super.init()
self.locationManager = CLLocationManager()
guard let locationManager = self.locationManager else {
return
}
if #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates = true
} else {
// Fallback on earlier versions
};
if CLLocationManager.authorizationStatus() == .NotDetermined {
// you have 2 choice
// 1. requestAlwaysAuthorization
// 2. requestWhenInUseAuthorization
locationManager.requestAlwaysAuthorization()
}
locationManager.desiredAccuracy = kCLLocationAccuracyBest // The accuracy of the location data
locationManager.distanceFilter = 2000 // The minimum distance (measured in meters) a device must move horizontally before an update event is generated.
locationManager.delegate = self
}
func startUpdatingLocation() {
print("Starting Location Updates")
self.locationManager?.startUpdatingLocation()
}
func stopUpdatingLocation() {
print("Stop Location Updates")
self.locationManager?.stopUpdatingLocation()
}
// CLLocationManagerDelegate
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [C
Solution
LocationManager Singleton
It is not necessary to use
This pattern can be achieved by doing the following:
For example:
From Apple’s documentation on Type Properties:
"Stored type properties are lazily initialized on their first access. They are guaranteed to be initialized only once, even when accessed by multiple threads simultaneously, and they do not need to be marked with the lazy modifier.”
The
Ultimately these changes make your code easier to read, ensure you have Singleton and are inline with Apple’s documented approach to Singletons using Swift.
Private CLLocationManager
Your
Self
In the following functions, the use of
CLLocationManager Desired Accuracy
You can remove the following code from your initializer:
On iOS,
Accuracy Constants
In terms of accuracy, you have six options:
At first glance one may think to use
Apple’s
When deciding which constant to use, careful consideration will be required based on the app’s intended use and the user’s expectations (and delivering a great mobile experience). I recommend reading the
Apple’s
It is not necessary to use
dispatch_once to make LocationService a Singleton.This pattern can be achieved by doing the following:
- Exposing a
statictype property that creates a new instance ofLocationService
- Marking the initializer of the class as
private
For example:
class LocationService: NSObject, CLLocationManagerDelegate {
static let shared = LocationSerivce()
// This class cannot be instantiated outside of this class
override private init() {
// Initialization code
}
}shared is lazily instantiated. A performance benefit that you inherit by implementing the Singleton this way.From Apple’s documentation on Type Properties:
"Stored type properties are lazily initialized on their first access. They are guaranteed to be initialized only once, even when accessed by multiple threads simultaneously, and they do not need to be marked with the lazy modifier.”
The
private initializer is key to the implementation of the Singleton pattern because it prevents anyone from outside of the class from creating another instance. That said, under other circumstances you may still want instances to be created to give your consumer more choices/functionality.Ultimately these changes make your code easier to read, ensure you have Singleton and are inline with Apple’s documented approach to Singletons using Swift.
Private CLLocationManager
Your
LocationSerivce appears to be a wrapper around Apple’s CLLocationManager class. It would probably be best to mark this constant as private. This prevents outside code from changing the reference or state of your locationManager property.private let locationManager = CLLocationManager()Self
In the following functions, the use of
self is not required. The reference to locationManager is not ambiguous and self is not required to explicitly state otherwise.func startUpdatingLocation() {
print(“Starting Location Updates”)
self.locationManager.startUpdatingLocation()
}
func stopUpdatingLocation() {
print(“Stop Location Updates”)
self.locationManager.stopUpdatingLocation()
}CLLocationManager Desired Accuracy
You can remove the following code from your initializer:
locationManager.desiredAccuracy = kCLLocationAccuracyBestOn iOS,
kCLLocationAccuracyBest is the default value of the desiredAccuracy property.Accuracy Constants
In terms of accuracy, you have six options:
kCLLocationAccuracyBestForNavigation
kCLLocationAccuracyBest
kCLLocationAccuracyNearestTenMeters
kCLLocationAccuracyHundredMeters
kCLLocationAccuracyAccuracyKilometer
kCLLocationAccuracyThreeKilometers
At first glance one may think to use
kCLLocationAccuracyBestForNavigation, however because increasing the accuracy requires more device resources (like GPS or Cellular Radio), it also means that more device power will be required. This can have a negative impact on the user experience if expectations are not set. There are also some other caveats that should be understood when setting the desiredAccuracy property. Apple’s
desiredAccuracy documentation: https://developer.apple.com/reference/corelocation/cllocationmanager/1423836-desiredaccuracyWhen deciding which constant to use, careful consideration will be required based on the app’s intended use and the user’s expectations (and delivering a great mobile experience). I recommend reading the
CLLocationManager documentation to better understand its usage and how to strike a good balance between your app’s functionality and device performance.Apple’s
CLLocationManager documentation: https://developer.apple.com/reference/corelocation/cllocationmanagerCode Snippets
class LocationService: NSObject, CLLocationManagerDelegate {
static let shared = LocationSerivce()
// This class cannot be instantiated outside of this class
override private init() {
// Initialization code
}
}private let locationManager = CLLocationManager()func startUpdatingLocation() {
print(“Starting Location Updates”)
self.locationManager.startUpdatingLocation()
}
func stopUpdatingLocation() {
print(“Stop Location Updates”)
self.locationManager.stopUpdatingLocation()
}locationManager.desiredAccuracy = kCLLocationAccuracyBestContext
StackExchange Code Review Q#135348, answer score: 3
Revisions (0)
No revisions yet.