NSXMLParser Cache - ios

I'm developing an application that uses the xml parsing. Everything works wing perfection but when I edit the xml file changes are not displayed. I think it may be a matter of cache because the file is loaded correctly on the server. how do I fix it?
-(IBAction)avviaParsing{
NSString *urlstring=#"http://www.acicastelloonline.it/app_ios/show.xml";
NSURL *xmlURL=[NSURL URLWithString:urlstring];
NSXMLParser *parser = [[ NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[parser setDelegate:self];
[parser parse];
[parser release];}

here is three option, you should try this code before XMLParsing:
you select only one of the three.
clearing
[[NSURLCache sharedURLCache] removeAllCachedResponses];
disable cache
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:cache];
[sharedCache release];
this is other solution.
NSURL *xmlURL=[NSURL URLWithString:URL];
NSURLRequest *request = [NSURLRequest requestWithURL:xmlURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:0.0f];
NSXMLParser *xmlParser =[[NSXMLParser alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

Related

Caching the downloaded data

I am using collection View to display the product data in each cell. Each time when user refreshes collectionview, it fetches data from the server again.
I need to know is there a caching mechanism available which caches downloaded images. I want to download only recently updated products which are not cached.
NSURL *url = [NSURL URLWithString:#"http://www.myURL/productAll.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSError *myError = nil;
id res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];
items = [[NSMutableArray alloc] init];
for (int i=0; i<[res count]; i++) {
NSString *arrayResult = [[res objectAtIndex:i]objectForKey:#"image"];
NSData *data = [[NSData alloc] initWithBase64EncodedString:arrayResult options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *captcha_image = [[UIImage alloc] initWithData:data];
[items addObject:captcha_image];
}
[collectionView reloadData];
}];
Use NSURLCache to setup memory and disk cache like this:
int cacheSizeMemory = xxxxx; //in memory cache
int cacheSizeDisk = xxxxx; //disk cache
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:#"nsurlcache"];
[NSURLCache setSharedURLCache:sharedCache];

Local HTML Cache policy in UIWebView

I have walked through various solution to clear the local image cache in UIWebView, while i trying to load the image in html it atomically displays the previous image(i using the same name to replace the image in template).
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL fileURLWithPath:[[CustomFunctions getFilesPath] stringByAppendingPathComponent:obj]] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
[webview1 loadRequest:request];
[webview1 reload];
this is my code and can any one please suggest me to do this. Thanks in advance. sorry for my English.
//to prevent internal caching of webpages in application
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
//[sharedCache release];
sharedCache = nil;
try using this. It will clear the url cache memory of your application.
Try this ...
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];
This would remove a cached response for a specific request. There is also a call that will remove all cached responses for all requests ran on the UIWebView:
[[NSURLCache sharedURLCache] removeAllCachedResponses];

Trying to understand NSURLCache and why I keep consuming memory

My app makes a lot of URL requests (calling a web service) in a loop for an extended period of time. When I watch the app in the Allocations tool, I see memory consumption going up continuously during the run of that loop. For testing purposes, I've reduced the loop to the following, which exhibits the same behavior:
NSURL *myUrl = [[NSURL alloc] initWithString:#"http://my.server.com/webservice"];
NSURLRequest request = [[NSURLRequest alloc] initWithURL:myUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
while (1)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *rawResponse = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[pool release];
}
When I first discovered the problem, I figured it was because my URL requests/responses were being cached. That's when I added the [[NSURLCache sharedURLCache] removeAllCachedResponses]. I was expecting that to clear out the cache after each call to the web service and free up any memory being used for URL caching purposes. No luck.
I must be doing something wrong, but I can't find it. Am I barking up the wrong tree in thinking it's the URL caching? What else could it be?

Can't clear UIWebView cache

I can't seem to find a way to clear UIWebView cache. I have tried the following, but nothin worked so far:
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:_request];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
_request = nil;
[NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
_request = [NSMutableURLRequest requestWithURL:t_url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
// also tried NSURLRequestReloadIgnoringLocalAndRemoteCacheData and NSURLRequestReloadIgnoringCacheData
timeoutInterval:10.0];
[_request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
[_request setHTTPShouldHandleCookies:NO];
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return nil; // Actually never gets called
}
Anybody came across this? Thanks!
You seem to have tried almost everything. I'm aware you are rejecting the cookies. Yet, Why dont you try this? This worked for me...
NSHTTPCookie *aCookie;
for (aCookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:aCookie];
}

Leaks in NSURLConnection

I have googled a lot and read a lot about the leaks issue with NSURLConnection, but none of them gives a definite answer as to what is the solution to overcome these leaks.
I create an asynchronous connection and every time the connection is established, I get a GeneralBlock-3584 leak. Sometimes the responsible library is shown to be Foundation [NSThread start] frame. Some instances of GeneralBlock-3584 have CFNetwork HostLookup_Master::HostLookup_Master(__CFString const*, InheritEnum<_ExtendedHostInfoType, CFHostInfoType>, __CFHost*, CFStreamError*) as responsible frame.
I tried setting NSURLCache size to 0 as suggested by some. Howvever, even that doesn't work.
Here is what my connector class looks like:
-(void) connectToUrl:(NSString*)urlStr withDelegate:(id)theDelegate{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"GET"];
[request setValue:#"application/xml" forHTTPHeaderField:#"Content-Type"];
self.delegate = theDelegate;
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[NSURLConnection connectionWithRequest:request delegate:self];
[pool release];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
if(xmlResponse == nil){
xmlResponse = [[NSMutableString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
}
else{
NSMutableString *temp = [[NSMutableString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
[xmlResponse appendString:temp];
[temp release];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[self.delegate connectionDidFinish:self];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
[self.delegate connectionDidFail:self];
}
And I am calling this connector class's connectToUrl:(NSString*)urlStr withDelegate:(id)theDelegate method as below:
Connector *con = [[Connector alloc] init];
[con connectToUrl:urlStr withDelegate:self];
I am releasing 'con' in connectionDidFinish and connectionDidFail methods of delegate class.
Please suggest a solution for the GeneralBlock-3584 leaks. I have been racking my brains for long.
The leaks have gone away since iOS4.

Resources