3

I'm making application using VK iOS SDK (russian social network). I'm sending request for getting some data from wall. I want to caching that data, but I don't really understand what should I do next. In AppDelegate I'm make:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
                                                             diskCapacity:20 * 1024 * 1024
                                                                 diskPath:nil];
    [NSURLCache setSharedURLCache:URLCache];
    return YES;
}

NSURLCache working in background, right? (kind of)

What next? I want to use NSURLRequestReturnCacheDataElseLoad. If I'm understand this right, it means: if I have cache, I'll use cache. But if it's not, I'll send request.

If I'm wrong, please correct me, where. I would be grateful.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
mr.gray
  • 53
  • 6
  • Maybe this article at NSHipster can help you? It describes how it works and how it should be used. http://nshipster.com/nsurlcache/ – Josip B. May 15 '15 at 09:44
  • Already read it, but I'm badly understand it. Where I should set (or create) requestCachePolicy property? As I understand it, it is sufficient setting a shared NSURLCache in -application:didFinishLaunchingWithOptions for working with cache. – mr.gray May 15 '15 at 10:11
  • It depends how you are making NSURLRequests. Basically NSURLCache stores NSURLRequest using storeCachedResponse: forRequest. So when your request finishes with loading you should save it using above mentioned method. When you want to read cached request you can use cachedResponseForRequest to check if request exists in cache. AFNetworking uses NSURLCache by default and maybe it can save you a lot of work. – Josip B. May 15 '15 at 10:36

1 Answers1

3

Use of the NSURLCache is automatic by AFNetworking and its underlying system libraries. As long as the responses from the server comply with RFC2616 in regards to the caching headers, your requests should use the cached data automatically. So the first step is to determine if that's the case. Other wise you've got a long road ahead of you to implement manual caching, though I imagine there's already a library to do that.

Jon Shier
  • 12,200
  • 3
  • 35
  • 37