Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
I am trying to implement caching in my RSS feed application. The application has many default RSS feeds implemented, and I want it to cache the data it downloads from each server and associate with the URL related, so that the next time the user wants to view that feed again, he will not have to download the file.

The question is... how do I do that in a most efficient way?

I initially thought of writing the complete raw NSData chinks I download from a server to the iPhone's disk, and then load it again when necessary, but how can I associate each cache file with a URL so that I can search if the URL the user sees at runtime is cached? I'm looking for ideas, and directions to that, rather than plain code.

Thank you in advance.
 
Could always make a sqlite3 table, table_pk, url_data, url and do the lookups based on the url field?
 
I initially thought of writing the complete raw NSData chinks I download from a server to the iPhone's disk, and then load it again when necessary, but how can I associate each cache file with a URL so that I can search if the URL the user sees at runtime is cached?

It depends on the number of items. If small to medium (up to ~1000), then NSDictionary. Otherwise mysql table.

Since a cache is designed to be small, and not contain every item forever, I'd start with NSDictionary and use that until it proves to be inadequate. It's one of the simplest things that could possibly work, and there's no point in adding mysql complexity until it's necessary.

Store the NSDictionary as a plist file. Each item's URL is the key, the pathname of a local unique file is the string value.

And it should go without saying, but I'll say it anyway: wrap it all inside a class, so the choice of NSDictionary vs. mysql is completely hidden from all other classes.
 
Thank you all, your responses were very helpful.

chown33, it shouldn't be more than 150~ associations. I mean... how many URLs can a user watch a day? :) Using an NSDictionary is certainly a good idea.

However, can't this be done using Core Data? My application already uses Core Data, and it should be fairly easy to add an entity called "CachedFile" that holds the cached file locations, dates and URLs. The NSDictionary method is also good, but I think that using the available and already set up Core Data database would prove faster, and more memory efficient.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.