Search code examples
swiftmkmapviewurlsession

Adding annotations to `MKMapView` from `URLSession` data


I am trying to add annotations to MKMapView of a single view app. Coordinates for annotations come from JSON data I fetch with a URLSession.dataTask and it occurs to me that the map view is updated faster than the data is fetched, parsed and put in a relevant array. How should I tackle this issue? Pieces of relevant map view code below.

import UIKit
import MapKit
import CoreLocation

class MapViewController: UIViewController {
    // MARK: - Properties
    @IBOutlet weak var mapView: MKMapView!
    let locationManager = CLLocationManager()
    var userLocation: CLLocation?
    var sensors = [Sensor]()
    
    let mapCenter = CLLocationCoordinate2D(latitude: 60.227704, longitude: 24.983821)
    var region = MKCoordinateRegion()

    // MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()

        let endpoint = "uiras2_v1"
        let url = uirasURL(with: endpoint)

        self.performRequest(url: url)
        
        self.mapView.addAnnotations(self.sensors)
        region = MKCoordinateRegion(center: mapCenter, latitudinalMeters: 8000, longitudinalMeters: 25000)
        mapView.setRegion(mapView.regionThatFits(region), animated: true)
        mapView.showsUserLocation = true
    }
    
    // MARK: - TODO data fetching
    func performRequest(url: URL) {
        // create URLSession
        let session = URLSession(configuration: .default)
            
        // give session task
        let task = session.dataTask(with: url) { (data, response, error) in
            if error != nil {
                print(error as Any)
                return
            }
                
            if let safeData = data {

                self.parseJSON(responseData: safeData)
            }
        }
        // start the task
        task.resume()
    }

    func parseJSON(responseData: Data) {
        let decoder = JSONDecoder()
        do {
            let decodedData = try decoder.decode(Response.self, from: responseData)
            for sensor in decodedData.sensors {
                sensors.append(sensor.value)
            }
        } catch {
            print(error)
        }
    }

Solution

  • You could just add the annotations right after it is being decoded.

    let decodedData = try decoder.decode(Response.self, from: responseData)
    DispatchQueue.main.async {
        mapView.addAnnotations(decodedData.sensors)
    }