0

I want to cache my PDF data (displayed using UIWebView) for improved app performance. I can't seem to find a working code or maybe it's just my code that doesn't work. My cache always returns NULL.

I have two viewcontrollers: NavigatePDFViewController and PDFViewController. The NavigatePDFViewController displays a list of PDF titles in which when clicked, opens a PDF file from the internet. The PDFViewController displays the PDF file.

Here are the steps I've done:

1) Added the NSCacheDelegate on my PDFViewController

2) Created an instance of NSCache like this. I created NSCache *cache in the NavigatePDFViewController.h.

cache = [[NSCache alloc]init];
[cache setDelegate:self];

3) Added an object on my cache like this. receivedData is an NSMutableData and link is an NSString.

[cache setObject:receivedData forKey:link];

4) When I check the content of my cache using the code below, it returns null.

[cache objectForKey:link]

What am I missing? Any help would be much appreciated.

haifacarina
  • 1,212
  • 1
  • 12
  • 18

1 Answers1

0

The docs for NSCache say:

A common data type stored in NSCache objects is an object that implements the NSDiscardableContent protocol. Storing this type of object in a cache has benefits, because its content can be discarded when it is not needed anymore, thus saving memory.

Is it possible that your receivedData implements that protocol? If so, it could have been discarded and you'll have to re-create it.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94