0

I am using NSURLSession to call my own Webservice which returns JSON, works fine with this code:

func getJSONFromDatabase(){
    let url = NSURL(string: "http://www.myurl/mysqlapi.php")

    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
        self.json = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print(self.json)
    }
    task.resume()
}

However, it seems that this Task is not executed in the right order, because when i run the following function after the "getJSONFromDatabase" function, the "print" statement in the Task is executed after the "print" statement from the "parseJSON" function.

func parseJSON(){

    let data = self.json.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

    do {
        let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! NSArray

        for event in json {
            let name = event["name"]
            let startDate = event["startDate"]


            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd"
            let date = dateFormatter.dateFromString(startDate as! String)


            if date != nil {
                self.events.append(Event(name: name as! String, startDate: date!))
            }
            else {
                print("Date is nil")
            }
        }

        for event in self.events {
            print(event.name)
        }
    } catch let error as NSError {
        print("Failed to load: \(error.localizedDescription)")
    }
}

My goal is to save the JSON Data in an Object Array of "Event", but this doesn't seem to work because when i iterate over my "self.events" array, it is empty.

Another problem is: When i split this 2 things like i posted here (2 functions), the "parseJSON" throws an error:

Failed to load: The data couldn’t be read because it isn’t in the correct format.

However, if i add the content of "parseJSON" into the Task of the "getJSONFromDatabase" function, there is no such error, but the array is still empty

3
  • 1
    Two things to fix: 1- NSURLSession.sharedSession().dataTaskWithURL is asynchronous so it will execute in the background while on the main thread your app is now calling parseJSON(). you'll have to use a "completionHandler" (many examples on SO). 2- NSURLSession.sharedSession().dataTaskWithURL gives you data but you transform it to a String and then in parseJSON() you transform it back to data! Not really efficient nor necessary. Just use the data. Commented Oct 26, 2015 at 11:10
  • 1
    Simple example for NSURLSession: stackoverflow.com/questions/31805045/… And for a completionHandler: stackoverflow.com/questions/26569724/… Commented Oct 26, 2015 at 11:11
  • call parseJSON in block after print(self.json) Commented Oct 26, 2015 at 11:18

1 Answer 1

2

dataTaskWithURL is asynchronous so you your code won't execute from the top down. Use a completion handler to work on the result from the asynchronous call.

func getJSONFromDatabase(success: ((json: String)->())){
    let url = NSURL(string: "http://www.myurl/mysqlapi.php")

    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
        let json = NSString(data: data!, encoding: NSUTF8StringEncoding)
        success(json: json)
    }
    task.resume()
}

in use

getJSONFromDatabase(success: {json in 
    //do stuff with json
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.