swift - 如何缓存 PDF 文档?
问题描述
我正在尝试缓存从 Firebase 下载的 PDFDocument,但它不工作。我正在使用 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。
为什么缓存不适用于此文件?
解决方案
推荐阅读
- tensorflow - 如何在 Tensorflow 2 中合并策略的结果
- beautifulsoup - 使用 BeautifulSoup 解析电影脚本 - 如何忽略嵌套在文本中的标签?
- javascript - 传递 props:其中一个 props `key` 是未定义的,但另一个是可读的
- php - 您如何使用 get_terms 列出没有子项的子分类法和父分类法?
- python - 有没有办法让 For Loop 迭代在一定时间后继续?
- javascript - 为什么这会发送相同的图像?
- kubernetes - 基于命名空间的单独filebeat守护进程集?
- xml - 使用 Zapier 将 XML 发送到 URL
- r - facet_wrap 并将颜色分配给ggplot2中的分类变量
- sql - 需要一条 SQL 语句来返回按常用代码分组的 MAX of Computed Value