0

I have a collection view application which when a cell is tapped on it loads up a pdf document in another UIViewController. These pdf files are found on a remote server. I am trying to cache these files using NSCache but without success.

Every time I tap on a cell I am getting the print from the console

PDF DOWNLOADED 

UIViewController displaying the pdf document:

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
}
dean
  • 321
  • 3
  • 17

0 Answers0