【问题标题】:PDFKit background searchPDFKit 后台搜索
【发布时间】:2017-10-05 11:15:51
【问题描述】:

我正在尝试使用新的 iOS PDFKit 框架在后台线程上运行搜索。

override func main() {
    if isCancelled {
      return
    }
    pdfDocument = PDFDocument.init(url: book.document.url)!
    pdfDocument.delegate = self
    pdfDocument.beginFindString("test", withOptions: [.caseInsensitive, .diacriticInsensitive]) (async)
    //pdfDocument.findString("test", withOptions: [.caseInsensitive, .diacriticInsensitive]) (sync)

  }

问题是没有调用 PDFDocumentDelegate 的任何方法,如果我使用 TIME Profiler,似乎什么也没有发生。 同步选项有效,但无法取消

有什么想法吗?

【问题讨论】:

    标签: ios swift nsoperationqueue nsoperation


    【解决方案1】:

    委托方法适用于同步findString

    在异步beginFindString 的情况下,您应该依赖通知:

    // Objective - C
    PDFDocumentDidBeginFindNotification
    PDFDocumentDidEndFindNotification
    PDFDocumentDidBeginPageFindNotification
    PDFDocumentDidEndPageFindNotification
    PDFDocumentDidFindMatchNotification
    

    // Swift
    Notification.Name.PDFDocumentDidBeginFind
    Notification.Name.PDFDocumentDidEndFind
    Notification.Name.PDFDocumentDidBeginPageFind
    Notification.Name.PDFDocumentDidEndPageFind
    Notification.Name.PDFDocumentDidFindMatch
    

    【讨论】:

    • 问题是beginFindString 在后台线程上什么都不做,只调用了func documentDidBeginDocumentFind(_ notification: Notification)。在主线程上一切正常。我的猜测是异步搜索有一个线程保护。我也尝试使用委托和通知。
    • 是的。似乎 beginFindString: 的异步方面根本不起作用。当我开始测试非常大的 PDF 时,我注意到这是一个问题。由于 beginFindString: 默认在主队列上运行,并且如果显式但在不同的队列上调用通知则失败,因此该方法毫无价值。
    猜你喜欢
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多