Как кешировать PDFDocument? - PullRequest
       85

Как кешировать PDFDocument?

1 голос
/ 12 апреля 2020

Я пытаюсь кэшировать PDFDocument, загруженный из Firebase, но он не работает. Я использую PDFKit для отображения PDF.

Вот код, который у меня есть:

    let cache = NSCache<NSString, PDFDocument>()

    func downloadPDF() {

    var thePdf = PDFDocument()

    if let cachedPdf = cache.object(forKey: "CachedPdf") {
         thePdf = cachedPdf
         self.pdfView.document = thePdf
         print("retrieved from cache")
     } else {

        //download
        guard let record = getRecord else {return}
        let storage = Storage.storage().reference(forURL: record.storageUrl)
        storage.downloadURL { (url, error) in
            if error == nil {

                if let getUrl = url {
                    thePdf = PDFDocument(url: getUrl) ?? PDFDocument()
                    self.cache.setObject(thePdf, forKey: "CachedPdf")
                    self.pdfView.document = thePdf
                    print("downloaded")
                }

            } else {
                guard let err = error else { return }
                self.alert(title: "Error", message: err.localizedDescription)
            }

        }

     }
}

Он никогда не сохраняет и не извлекает что-либо из кэша, он всегда загружает PDF.
Почему кеш не работает для этого файла?

...