royvano royvano - 2 months ago 10
iOS Question

Having trouble getting my info out datatask swift 3

I have this function, and I want to fill the locations array with latitude/longitude data that I get in the dataTask function.

The problem is that the data is only available in the datatask function and outside it, it'll be gone.

func getFromDatabase()
{
var locations: [CLLocationCoordinate2D] = []
let url = URL(string: URL_DATABASESend)

let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
//Here I print the JSON: print(NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!)
do{
if(data != nil){
let parsedData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [[String:String]]
for dic in parsedData! {
if let lat = Double(dic["latitude"]!), let long = Double(dic["longitude"]!) {
//print(lat)
//print(long)
var coordinatesToAppend = CLLocationCoordinate2D(latitude: lat, longitude: long)
locations.append(coordinatesToAppend)
}
}
}} catch let error as NSError{
print(error.localizedDescription)
return
}
}
task.resume()
}


I don't know how to use a completionhandler and when I do I get a lot of errors (I followed a lot of tutorials/online help topics).

Does anyone know how to I can get the information out of the function?(can't return the value cause I'm using datatask) I read a lot of topics but none of it solved my problem.

Hope to hear something from you guys!

Answer

You need to use a callback to get your content from the closure.

Your content is this array: var locations: [CLLocationCoordinate2D] so we are going to use a callback like this one:

completion: @escaping ([CLLocationCoordinate2D])->()

We add it to the method signature:

func getFromDatabase(completion: @escaping ([CLLocationCoordinate2D])->())

Then we use it in your code where the content is ready, just after the loop:

func getFromDatabase(completion: @escaping ([CLLocationCoordinate2D])->())
{
    var locations: [CLLocationCoordinate2D] = []
    let url = URL(string: URL_DATABASESend)

    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
        do {
            if data != nil {
                let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [[String:String]]
                for dic in parsedData {
                    if let lat = Double(dic["latitude"]!), let long = Double(dic["longitude"]!) {
                        let coordinatesToAppend = CLLocationCoordinate2D(latitude: lat, longitude: long)
                        locations.append(coordinatesToAppend)
                    }
                }
                // Here the array is ready, we use the completion handler
                completion(locations)
            }
        } catch let error as NSError {
            print(error.localizedDescription)
            return
        }
    }
    task.resume()
}

Then you call your method with a trailing closure like this to get back the array:

getFromDatabase { (locs) in
    // Here "locs" is your [CLLocationCoordinate2D] array
    print(locs)
}