0
let task = session.dataTaskWithURL(url!, completionHandler: {
    data, response, error -> Void in

    if (error != nil) {
        println(error)


    } else {


        let jsonresult = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary

        var dummyfeed:AnyObject

       //println(jsonresult)
        for var i = 0; i <  jsonresult["feed"]!.count; i++ {

            self.feeds.append([String:String]())

            dummyfeed = jsonresult["feed"]![i] as NSDictionary

           self.feeds[i]["id"] = dummyfeed["id"] as? String
           self.feeds[i]["name"] = dummyfeed["name"] as? String
           self.feeds[i]["status"] = dummyfeed["status"] as? String
           self.feeds[i]["profilePic"] = dummyfeed["profilePic"] as? String
            self.feeds[i]["timeStamp"] = dummyfeed["timeStamp"] as? String
            self.feeds[i]["url"] = dummyfeed["url"] as? String

        }

    }
})
task.resume()

So Feeds is a global variable, so that I display the picture of each entry in Feeds on a table view. But it's calling asynchronously println(self.feeds) inside the task variable and println(feeds) outside of the task variable are differnent. How do I make it synchronously?

3
  • ummm how do you do that... I am an amateur programmer lool Commented Mar 15, 2015 at 21:16
  • Hey Rob, my updated code is posted on the answer section. I am still struggling Commented Mar 15, 2015 at 22:02
  • I suggest you use Alamofire and SwitfyJSON instead use this method. Commented Mar 16, 2015 at 5:26

3 Answers 3

1

Do not make it run synchronously. Run it asynchronously, but then synchronize the interaction with feeds. The simplest way to achieve that it to dispatch the updating of the feeds back to the main queue and reloadData for the table view. This eliminates the possibility that you'll be using it from the main queue while it's mutating in the background, but avoids the horrible UX of doing this network request synchronously:

let task = session.dataTaskWithURL(url!) { data, response, error in
    if (error != nil) {
        println(error)
    } else {
        var parseError: NSError?
        if let jsonresult = NSJSONSerialization.JSONObjectWithData(data, options:nil, error: nil) as? NSDictionary {
            if let receivedFeeds = jsonresult["feed"] as? [[String: AnyObject]] {
                dispatch_async(dispatch_get_main_queue()) {
                    self.feeds = [[String: String]]()
                    for receivedFeed in receivedFeeds {
                        var outputFeed = [String : String]()
                        outputFeed["id"] = receivedFeed["id"] as? String
                        outputFeed["name"] = receivedFeed["name"] as? String
                        outputFeed["status"] = receivedFeed["status"] as? String
                        outputFeed["profilePic"] = receivedFeed["profilePic"] as? String
                        outputFeed["timeStamp"] = receivedFeed["timeStamp"] as? String
                        outputFeed["url"] = receivedFeed["url"] as? String
                        self.feeds.append(outputFeed)
                    }
                    self.tableView.reloadData()
                }
            } else {
                println("did not find `feed`")
            }
        } else {
            println("problem parsing JSON: \(parseError)")
        }
    }
}
task.resume()

That should be a little more robust handling errors and employs asynchronous pattern of letting request run asynchronously, but dispatch updating of model object and UI back to the main thread.

Sign up to request clarification or add additional context in comments.

Comments

0
  let task = session.dataTaskWithURL(url!, completionHandler: {
        data, response, error -> Void in

        if (error != nil) {
            println(error)


        } else {


            let jsonresult = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary

            var dummyfeed:AnyObject

           //println(jsonresult)
            for var i = 0; i <  jsonresult["feed"]!.count; i++ {

                self.feeds.append([String:String]())

                dummyfeed = jsonresult["feed"]![i] as NSDictionary
                dispatch_async(dispatch_get_main_queue()) {

                    self.feeds[i]["id"] = dummyfeed["id"] as? String
                    self.feeds[i]["name"] = dummyfeed["name"] as? String
                    self.feeds[i]["status"] = dummyfeed["status"] as? String
                    self.feeds[i]["profilePic"] = dummyfeed["profilePic"] as? String
                    self.feeds[i]["timeStamp"] = dummyfeed["timeStamp"] as? String
                    self.feeds[i]["url"] = dummyfeed["url"] as? String
                }

            }
            self.tableView.reloadData()
        }

    })

        task.resume()

Hey Rob, I did what I think you tell me to do, and feeds is still empty :(

3 Comments

When you say "feeds" is empty, what do you mean? Where are you checking it? Don't worry if it's initially nil, but make sure it's not nil by the time reloadData is called.
it's not nil; I get "[[:]]". I typed println(feeds) outside of the task variable and I get that
That block runs asynchronously (i.e. later). Thus, if you look at it outside the task variable, the request will not have finished and thus it will be that empty array that you describe. Put a println/NSLog inside the closure and then again after it, and you'll see that the one inside the closure happens later. That's why you do the reloadData inside the closure, so that when the data is retrieved later, the table can be reloaded (even though it was initially configured when the tableview was first instantiated).
0

I have same problem my code is was working fine, but now, using dataTaskWithURL it didn't return any data, or even error. I think issue is iOS 8.2 I upgraded.

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.