1

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...

Wiingaard
  • 4,150
  • 4
  • 35
  • 67
  • 1
    Check this out: [Is key-value observation (KVO) available in Swift?](http://stackoverflow.com/questions/24092285/is-key-value-observation-kvo-available-in-swift) – 0x416e746f6e Jan 07 '16 at 13:05
  • Thanks, I did check it out, and tried to implement it, but it didn't work for me.. See the edit section.. – Wiingaard Jan 07 '16 at 13:49

1 Answers1

2

Change var currentLocation to dynamic var currentLocation. You must add the dynamic modifier to any property you want to observe in KVO.

Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
Avaan
  • 4,709
  • 1
  • 10
  • 13