iphonecocoa-touchcocoanscache

Save NSCache Contents to Disk


I'm writing an app that needs to keep an in-memory cache of a bunch of objects, but that doesn't get out of hand so I'm planning on using NSCache to store it all. Looks like it will take care of purging and such for me, which is fantastic.

I'd also like to persist the cache between launches, so I need to write the cache data to disk. Is there an easy way to save the NSCache contents to a plist or something? Are there perhaps better ways to accomplish this using something other than NSCache?

This app will be on the iPhone, so I'll need only classes that are available in iOS 4+ and not just OS X.

Thanks!


Solution

  • I'm writing an app that needs to keep an in-memory cache of a bunch of objects, but that doesn't get out of hand so I'm planning on using NSCache to store it all. Looks like it will take care of purging and such for me, which is fantastic. I'd also like to persist the cache between launches, so I need to write the cache data to disk. Is there an easy way to save the NSCache contents to a plist or something? Are there perhaps better ways to accomplish this using something other than NSCache?

    You pretty much just described exactly what CoreData does; persistency of object graphs with purging and pruning capabilities.

    NSCache is not designed to assist with persistency.

    Given that you suggested persisting to a plist format, using Core Data instead isn't that big of a conceptual difference.