Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLCache, set custom key

Tags:

ios

nsurlcache

I'm using NSURLCache and I'd like to set a custom key for a NSURLRequest. Is that possible?

Complete explanation:

I'm developing a mapping app, that uses OpenStreetMap tiles. OpenStreetMap provides multiple servers to serve the tiles, to reduce the load on each server. I'm using these servers randomly. So for instance, the following URLs will give the same tile:

  • http://a.tile.openstreetmap.org/3/5/5.png
  • http://b.tile.openstreetmap.org/3/5/5.png
  • http://c.tile.openstreetmap.org/3/5/5.png

Obviously, it causes some problems for my caching, because if I cache a tile from server A, next time, if I try to load from server B, the NSURLCache won't find the tile.

So, I'd like to set myself the cache key, in order to handle that case. Is that possible?

like image 663
Tim Autin Avatar asked Mar 12 '23 01:03

Tim Autin


1 Answers

You can subclass NSURLCache and override the implementations of cachedResponseForRequest: and storeCachedResponse:forRequest: -- then use setSharedURLCache: to set it to your subclass.

The easiest thing is probably to call super for storing (or not override), but then on the lookup, if it's a tile request, check all the possibilities (using super) and return the response if you get a non-nil one.

like image 126
Lou Franco Avatar answered Mar 19 '23 19:03

Lou Franco