点击UICollectionViewCell在另一个UIViewController中查看PDF文档时,NSCache不缓存我的文件-Swift

时间:2018-09-06 13:44:29

标签: ios swift nscache

我有一个集合视图应用程序,当在其上点击一个单元格时,它将在另一个UIViewController中加载pdf文档。这些pdf文件可在远程服务器上找到。我正在尝试使用NSCache缓存这些文件,但是没有成功。

每次我点击一个单元格时,我都会从控制台获取打印内容

PDF DOWNLOADED 

UIViewController显示pdf文档:

let pdfCache = NSCache<NSString, AnyObject>.init()

func loadPDF(pdfURL: String) -> Void {


    // FIND IF PDF DOCUMENT IS ALREADY IN CACHE ON KEY 'pdfURL'
    if let documentToCache = self.pdfCache.object(forKey: pdfURL as NSString){
        self.myPDFView.document = (documentToCache as! PDFDocument)
        print("PDF FROM CACHE!")
        return
    }

    // SHOW LOADING CIRCLE
    self.showLoadingCircle(activityIndicatorView: self.loadingDisplay.activityIndicator, overlayView: self.loadingDisplay.overlay)

    Alamofire.request(pdfURL).responseData { response in

        if let data = response.data{

            DispatchQueue.main.async{

                let document = PDFDocument.init(data: data)

                // STORE PDF DOCUMENT TO CACHE
                self.pdfCache.setObject(document!, forKey: pdfURL as NSString)

                // DISPLAY PDF
                self.myPDFView.document = document
                print("PDF DOWNLOADED")

                // HIDE LOADING CIRCLE
                self.hideLoadingCircle(activityIndicatorView: self.loadingDisplay.activityIndicator, overlayView: self.loadingDisplay.overlay)
            }
        }
    }// END RSPONSE HANDLER
}

0 个答案:

没有答案