mr.gray
mr.gray

Reputation: 53

How to use NSURLCache?

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.

Upvotes: 3

Views: 1140

Answers (1)

Jon Shier
Jon Shier

Reputation: 12770

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.

Upvotes: 3

Related Questions