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 8022399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:15:29+00:00 2026-06-04T22:15:29+00:00

I am trying to cache my nsurlrequest data. Prior to this i was successfully

  • 0

I am trying to cache my nsurlrequest data. Prior to this i was successfully downloading data from the server, ever since I tried to implement this cache its been making my application fall over. Im hoping someone can look at the code i have so far and help me to get it workig.. I think I have roughly 95% of it done.

- (IBAction)setRequestString:(NSString *)string
{
    //Set database address
    NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"http://127.0.0.1:29/"]; // imac development


    //PHP file name is being set from the parent view
    [databaseURL appendString:string];

    //call ASIHTTP delegates (Used to connect to database)
    NSURL *url = [NSURL URLWithString:databaseURL];

    //SynchronousRequest to grab the data, also setting up the cachePolicy
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0]; //if request dose not finish happen within 60 second timeout.

    //This nsdata will be used to put the cache into
    NSData *cacheData = [[NSData alloc] init];
    //Create my own NSCachedURLResponse and add it to the cache
    NSURLResponse *responseToCache = [[NSURLResponse alloc] initWithURL:url MIMEType:@"text/xml" expectedContentLength:[cacheData length] textEncodingName:nil];
    //Set up the cache
    NSCachedURLResponse *cacheResponse = [[NSCachedURLResponse alloc] initWithResponse:responseToCache data:cacheData];
    [[NSURLCache sharedURLCache] storeCachedResponse:cacheResponse forRequest:request];

    //check if its really there
    NSLog(@"cache = %@", [[NSURLCache sharedURLCache] cachedResponseForRequest:request]);


    //If no cache do this stuff, connect to the db and perform these actions. I think this is what i am supposed to be doing.
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
        if ([data length] > 0 && error == nil){
            [self receivedData:data];
        }else if ([data length] == 0 && error == nil){
            [self emptyReply];
        }else if (error != nil && error.code == NSURLErrorTimedOut){
            [self timedOut];
        }else if (error != nil){
            [self downloadError:error];
        }
    }];
}
  • 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-04T22:15:31+00:00Added an answer on June 4, 2026 at 10:15 pm

    Follow this link for an dead simple way of doing this using a category method on NSURLConnection. It’s for asynchronous requests but can be adapted easily for synchronous ones.

    Also I strongly advise that you use a private property or instance variable for your NSOperationQueue so that all your requests use the same operation queue.

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

Sidebar

Related Questions

I'm trying to cache a page that gets/processes data from very slow API's -
I am trying to cache js files on server side and came across this
I'm trying to implement sql server 2005 cache dependency in ASP.Net 3.5. I have
I'm trying to cache a data row or tuple fetched from a SQL query
I'm trying to cache images I load from Flickr. If I load the same
I'm trying to cache some images loaded from a URL by using AFImageCache, without
I'm trying to cache results from several pages that do heavy database processing. These
I have some code(client side) that is trying to cache responses from a web
I'm trying to cache an ActiveRecord with its association. The problem is there's a
I'm trying to cache JSON content generated by a php script from database. However

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.