I have a singleton class that manages location in my app:
import UIKit
import CoreLocation
class Location: NSObject, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var currentLocation:CLLocationCoordinate2D? {
didSet {
self.locationManager.stopUpdatingLocation()
}
}
//////////////////////////////////////////////////////////////////////////////
class var manager: Location {
return UserLocation
}
//////////////////////////////////////////////////////////////////////////////
override init () {
super.init()
if self.locationManager.respondsToSelector(Selector("requestAlwaysAuthorization")) {
self.locationManager.requestWhenInUseAuthorization()
}
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = 50
}
//////////////////////////////////////////////////////////////////////////////
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if self.currentLocation == nil {
print("User Location NOT Updated.")
} else {
print("did update location")
}
self.currentLocation = manager.location!.coordinate
}
//////////////////////////////////////////////////////////////////////////////
func startLocationUpdate() {
self.locationManager.startUpdatingLocation()
}
//////////////////////////////////////////////////////////////////////////////
func stopLocationUpdate() {
self.locationManager.stopUpdatingLocation()
}
}
// Mark: - Singleton Location
//////////////////////////////////////////////////////////////////////////////
let UserLocation = Location()
So from anyother class I'm able to call:
let theCurrentLocation = UserLocation.currentLocation
But is it possible to add some observer to this property in another class?
or
Is it possible to notify another class, that the property has changed, in some other clever way?
I found the addObserver
method, addObserver(observer: NSObject, forKeyPath: String, options: NSKeyValueObservingOptions, context: UnsafeMutablePointer<Void>)
but wasn't sure this could be used in this context. I'm looking for something like this?
class AnotherCLass: NSObject {
override init() {
super.init()
// seudo ->
UserLocation.addObserver(self, UserLocation.currentLocation, "someAction:")
}
func someAction() {
print("currect location has changed..")
}
}
EDIT:
So I took a look at Key-Value Observing, and it sounds just like what I need. I followed the guide, but I dont get any notification when the property I want to observe is changed. So I add the observer like this:
UserLocation.addObserver(self, forKeyPath: "currentLocation", options: .New, context: nil)
And the observing method like this:
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
print("notification received")
if keyPath == "currentLocation" {
print("Current Location received")
}
}
But this method is never called, eventhough the ´currentLocation´ is changed...