I am trying to cache a PDFDocument downloaded from Firebase but it is not working. I am using PDFKit to display the pdf.
Here is the code I have:
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)
}
}
}
}
It never stores or retrieves anything from the cache, it always downloads the pdf.
Why is cache not working for this file?