使用Swiftyjson创建一个异步请求

时间:2015-04-23 18:21:53

标签: ios objective-c swift asynchronous swifty-json

我有以下3种不同名称的方法。然而,在他们被执行的那一刻,他们似乎阻止了UI。我想这样做是为了制作同步请求,那么如何才能使这成为异步请求呢?

func getPopular(url: NSString) {

    let popularUrl = NSURL(string: url as String)
    var popularRequest = NSURLRequest(URL: popularUrl!)
    var popularData = NSURLConnection.sendSynchronousRequest(popularRequest, returningResponse: nil, error: nil)

    let jsonArray = JSON(data: popularData!)
    if jsonArray.count < numberOfRecordsPerAPICall {
        popularCount = 0
        self.tableVIew.tableFooterView = nil
    } else {
        popularCount = jsonArray.count
        self.tableVIew.tableFooterView = footerView
    }
    for (key: String, subJson: JSON) in jsonArray {

        var httpUrl = subJson["image_url"].stringValue
        let url = NSURL(string: httpUrl)
        let data = NSData(contentsOfURL: url!)

        // Create an object and parse your JSON one by one to append it to your array
        var newNewsObject = News(id: subJson["id"].intValue, title: subJson["title"].stringValue, link: subJson["url"].stringValue, imageLink: UIImage(data: data!)!, summary: subJson["news_text"].stringValue, date: getDate(subJson["date"].stringValue))





        popularArray.append(newNewsObject)
    }

0 个答案:

没有答案