1

我有以下 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)
    }
4

0 回答 0