Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8896469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:03:46+00:00 2026-06-15T00:03:46+00:00

I am trying to use the disk based url cacing built in to iOS

  • 0

I am trying to use the disk based url cacing built in to iOS 5. I have a json feed which i want to load and then cache, so its instantly loaded the next time a user uses the app.
The cache.db file is successfully created and if i open it in a sqlite-editor i can fetch out the data.

I am using AFNetworking

NSURL *url = [NSURL URLWithString:@"http://myurl.com/json"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSLog(@"%d before call",[[NSURLCache sharedURLCache] currentDiskUsage]);  // logs 0 bytes
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// is successfully loading JSON and reading it to a table view in a view
}failure:nil];

NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request] 
// this returns nil, and can't load the request from disk.

In the cache response block, strangely the cache is now working, and successfully gets the cacheurl request from memory.

[operation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
        NSCachedURLResponse *cachedResponse_ = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
        NSLog(@"%d AFTER CACHE",[[NSURLCache sharedURLCache] currentDiskUsage]);
        // this writes out 61440
        return cachedResponse;
    }];

How can i load the cached URL request from disk the next time i start the app?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:03:51+00:00Added an answer on June 15, 2026 at 12:03 am

    When you attempt to fetch the cached response in your first code section (line 9), the request has not completed yet.

    -[AFJSONRequestOperation JSONRequestOperationWithRequest:success:failure:] is asynchronous, so nothing will be stored in the cache until the request completes. If you try to fetch the cached response inside the success block (or in a cache response block, as you did in your second code section), it should work correctly.

    As to your second question, you can’t guarantee that the data will be cached by NSURLCache, even if your Cache-Control headers state that your content shouldn’t expire until far in the future. The cache file is always stored in your app’s Caches directory, which can be cleared by iOS at any time if it thinks it needs to free up disk space.

    If you need to make sure your JSON data is always available, save it yourself in your app’s Library/Application Support directory, ideally with the NSURLIsExcludedFromBackupKey specified (unless your app truly cannot function without the data).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to use virsh attah-disk to attach a qcow2 file as
I'm trying to use a PEM(X.509) certificate (stored in a privateKey.pem file on disk)
I am trying use gem tire to search in my application. I have tables
Hi I'm trying use a datepicker on a field I have. I'm trying to
I'm trying to improve my build times and want to have some feedback in
I'm trying to use Kryo to read/write objects to disk: http://code.google.com/p/kryo/ Couple questions: How
I'm trying to use MODI to perfom OCR on bitmaps I already have in
I'm trying to use mmap to read and play audio files on iOS. It
i'm trying use facebook API to upload photo in my fan page. I downloaded
I was trying use a set of filter functions to run the appropriate routine,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.