How to use NSCachedURLResponse

I am trying to save different datasets that are returned to me via my NSURLConnection methods.

this is my connectionDidFinishLoading method, which is currently passing every single piece of data to the parser. As a side, I have no NSURLMutableRequest cache policy set to NSURLRequestUseProtocolCachePolicy

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{    
    if ([methodName isEqualToString:@"GetRub"]) 
    { 
        [engineResponses GetRub:receivedData];
    }
    else if ([methodName isEqualToString:@"GetMag"]) 
    {
        [engineResponses GetMag:receivedData];
    }
    else if ([methodName isEqualToString:@"GetSub"])
    {
        [engineResponses GetSub:receivedData];
    }
}

      

Re-cap: The above method will receive the requested dataset, and then depending on the request made, it will pass that data to the correct method in the parser class.

GetRub, GetMag, GetSub.

What I want to try to do is use the NSURLCacheResponse method to collect the data, as I did above, but then check for the presence of a cache, etc. if there is one, then pass the cached data to the parser, which will usually be about which we are talking about the above method.

Below I tried to use my cache. Instead of connectionDidFinishLoading returning data. I think I want this method to execute it, so I will try to call the engineResponses method from within this method after the cache has been sorted.

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    if ([methodName isEqualToString:@"GetRub"]) 
    { 
        NSCachedURLResponse *ICRub = cachedResponse;

        ICRub = nil;
        NSDictionary *newUserInfo;
        newUserInfo = [NSDictionary dictionaryWithObject:[NSDate date] forKey:@"Cached Date"];                                                                             
        ICRub = [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response] data:[cachedResponse data] userInfo:newUserInfo storagePolicy:[cachedResponse storagePolicy]];                                                                     

    }
//other caches below here.

      

So this is what I tried to do, but it is currently not working, it never does it for this method. Also I don't know how to send the cached data to the parser class. These are my main atm problems, another problem - do you think this is a very good solution?

any help would be greatly appreciated :)

+3


source to share





All Articles