UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Best approach to share changes between classes

Forums > Swift


Background I have a view (HomeView) where I want to show in a List only the elements that are nearby user location. On the other hand, I have another class to manage the location authorization and the location request (LocationManager). Finally, at HomeView, I create an instance of LocationManager class so the system requests location authorization to the user and perform the location request.

In your opinion, what would be the best approach from HomeView ViewModel class to be aware when the location data is ready in order to start collecting the List elements? Should I use the observer pattern? Is there an easier way?

LocationManager class:

class LocationManager: NSObject, CLLocationManagerDelegate {
    var locationManager = CLLocationManager()

    override init() {
        locationManager.delegate = self
        locationManager.distanceFilter = 10000
        locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
        locationManager.activityType = .other

    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        switch manager.authorizationStatus {
        case .notDetermined:
        case .restricted, .denied:
        case .authorizedWhenInUse:

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("Location: \(locations.first)")
        // How HomeView should be informed at this point that the location data is ready? <---------------

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Failed \(error)")



Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.