func loadDataSource() {
self.refreshControl!.beginRefreshing()
var loadURL = NSURL(string: hackerNewsApiUrl)
var request = NSURLRequest(URL: loadURL!)
// var error: NSError? = nil
var loadDataSourceQueue = NSOperationQueue();
NSURLConnection.sendAsynchronousRequest(request, queue: loadDataSourceQueue, completionHandler: { response, data, error in
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
let newsDataSource = json["item"] as! NSArray
var currentNewsDataSource = NSMutableArray()
for currentNews : AnyObject in newsDataSource {
let newsItem = XHNewsItem()
newsItem.newsTitle = currentNews["title"] as! NSString
newsItem.newsThumb = currentNews["thumb"]as! NSString
newsItem.newsID = currentNews["id"] as! NSString
currentNewsDataSource.addObject(newsItem)
print( newsItem.newsTitle)
}
dispatch_async(dispatch_get_main_queue(), {
self.dataSource = currentNewsDataSource
self.tableView.reloadData()
self.refreshControl!.endRefreshing()
})
}catch let error as NSError {
print(error)
dispatch_async(dispatch_get_main_queue(), {
self.refreshControl!.endRefreshing()
})
}
})
}