Matthew
Matthew

Reputation: 867

Using ASIDownloadCache offline

I want to get a JSON and cache it when the Internet is available or use cache if it's not. I used this code, but request's responseString is always null when I'm offline. (Documentation says it works only if server returns 200 or 300 response.

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];

Any idea to get the cached data when there's no network connection?

Thank you.

Upvotes: 3

Views: 1193

Answers (2)

user244343
user244343

Reputation:

You need to make sure the request is explicitly cached permanently (across application sessions). To set this:

[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];

Once the request is cached, it will immediately return the cached data as long as your download cache's policy falls back to the cache when a remote connection fails. [ASIDownloadCache sharedCache] should already be set to the default cache policy, which handles this case sufficiently well.

Upvotes: 2

Andy Riordan
Andy Riordan

Reputation: 1154

You can use this:

[[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:]

To get the path to the cache on disk. If there is no cached data, it will return nil.

Upvotes: 2

Related Questions