1

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!

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
rvano
  • 97
  • 7
  • 2
    Possible duplicate of http://stackoverflow.com/questions/31264172/how-can-i-get-the-data-from-nsurlsession-sharedsession-datataskwithrequest – Eric Aya Sep 28 '16 at 08:31
  • @EricAya I read that topic, but it didn't help me... Please help me I really want this to work, but haven't got the programming experience, I also sent you a tweet at twitter. – rvano Sep 28 '16 at 08:33
  • Can I chat with you @EricAya let me know how – rvano Sep 28 '16 at 08:37
  • I've posted an answer with explanations. – Eric Aya Sep 28 '16 at 08:45

1 Answers1

0

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)
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253