I just moved from ASIHTTP to AFNetworking library. I am also using SDURLCache library from Olivier Poitrey and Peter Steinberg. I want to cache all the images that I gonna use in my application. For these, I tried this:
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
for(NSURLRequest *imageRequest in imageRequestArray){
AFURLConnectionOperation *operation2 = [[[AFURLConnectionOperation alloc] initWithRequest:imageRequest] autorelease];
[queue addOperation: operation2];
[operation2 waitUntilFinished];
}
And when I need to show images, I am doing this for each image:
NSURL *url = [NSURL URLWithString:[cat imagePath]];
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30.0];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:imageRequest
imageProcessingBlock:nil
cacheName:@"nscache"
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
//NSData *responseData = [request responseData];
imageView.alpha = 0.0;
[imageView setImage:image forState:UIControlStateNormal];
[UIView beginAnimations:@"ToggleViews" context:nil];
[UIView setAnimationDuration:1.0];
imageView.alpha = 1.0;
[UIView commitAnimations];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){}
];
[operation start];
After a while, application gives memory warning, then shuts down. For that, I did the following:
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSLog(@"Memory Warning...");
}
It clears the cache(what I don’t what) but after a while, application closes again.
What should I do for that?
The problem is likely caused because ARC doesn’t automatically get set up on queues or on background threads. I had the same issue and solved it with the following article:
https://agilewarrior.wordpress.com/2012/04/09/memory-leaks-with-arc/