ios - 异步调用中的异步调用未执行

标签 ios swift swift2 nsurlsession

我正在尝试从 rss 提要中检索 XML,获取每篇文章的链接,然后从这些文章中提取信息。我正在使用 AEXML获取 xml,和 ReadabilityKit用于链接提取。

我已成功从 XML 中提取链接,但对可读性的解析器调用从未执行。我不想在主线程上这样做,因为它会阻塞所有 UI,但到目前为止,这是我让它工作的唯一方法。代码如下(删除了 dispatch get main queue):

func retrieveXML() {
    let request = NSURLRequest(URL: NSURL(string: "<XML URL HERE>")!)
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        (data, response, error) in

        if data == nil {
            print("\n\ndataTaskWithRequest error: \(error)")
            return
        }

        do {
            let xmlDoc = try AEXMLDocument(xmlData: data!)
            for child in xmlDoc.root.children {

                if let postURL = child["id"].value {

                    let url = NSURL(string: postURL)
                    let parser = Readability(url: url!)
                    let title = parser.title()
                    print("TITLE: \(title)")
                }
            }
        } catch {
            print(error)
        }
    }
    task.resume()
}

最佳答案

感谢您的报告。新版本可用于 cocoa pods 和带有新 aync API 的 cartage。同步 API 已从项目中删除。

Readability.parse(url: articleUrl, { data in
  let title = data?.title
  let description = data?.description
  let keywords = data?.keywords
  let imageUrl = data?.topImage
  let videoUrl = data?.topVideo
})

感谢您的贡献!有关更多信息,请查看自述文件 https://github.com/exyte/ReadabilityKit

关于ios - 异步调用中的异步调用未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39253599/

相关文章:

ios - 使用 xcodebuild 运行单个测试配置

swift - 'UIColor !' is not identical to ' SKColor'

swift - 在 cellforitem swift 中显示两种不同类型的单元格

ios - 如何使容器 View 背景透明而不是一切?

beta - 二元运算符 '&&' 不能应用于两个 Bool 操作数

swift2 - 用于条件绑定(bind)的 swift 2 初始化程序必须具有可选类型,而不是 'UIImage'

iOS - 在没有编辑模式的情况下允许在 `UITableView` 中进行默认行移动

ios - 如何在 Swift 中制作可设计的 UIImage

ios - 如何修复无法将类型 'AnyObject' 的值转换为预期参数类型 '_?' 错误?

node.js - JSONSerialization 生成不正确的 JSON 对象。 swift