3

I am trying to save different sets of data that is being returned to me via my NSURLConnection methods.

this is my connectionDidFinishLoading method, which currently at the moment is passing each separate lot of data over to a parser. As a side not I have a 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 method above receives the requested data set and then depending on the request made it will pass that data over to the correct method in the parser class.

GetRub, GetMag, GetSub.

What I want to try and do here is use the NSURLCacheResponse method to capture the data like I have done above but then check for a possible cache etc. if there is one then pass the cached data to the parser which would normally be taken care of by the method listed above.

Below is what I have tried for my cache. Instead of having connectionDidFinishLoading reciving the data.. I think I want this method to do it, so I will try to call the engineResponses method from inside this method once the cache has been sorted out.

- (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 thats what I have tried to do, but currently its not working, it never makes it to this method. Also I dont know how to send the cached data to my parser class.. those are my main issues atm, the other issue is do you think this is a very good solution?

any help would be greatly appreciated :)

C.Johns
  • 10,185
  • 20
  • 102
  • 156

0 Answers0