Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 01-27-2010, 10:58 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 96
blueFLoyd8 is on a distinguished road
Default Synchronous NSURL request can't cache

My app loads a large number of images from the server and displays them. I am using an operation queue to do the loading and a synchronous request within the operation to get the image (because asynchronous is not allowed apparently within an operation).

I am running into a cache problem. None of URL responses are getting cached and I do not know how to fix it. here is the code from my operation:

.h
Code:
@interface DRGetImageOperation : NSOperation {
	NSURL *objectURL;
	NSNumber *index;
	UIViewController *mainViewController;
	SEL callback;
}
@property (nonatomic, retain) NSURL *objectURL;
- (id)initWithViewController:(UIViewController *)viewController andIndex:(int)number;
- (void) setCallback:(SEL)select;
@end
.m
Code:
// Partial code. Some small methods are not shown
- (void)main {
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
	NSLog(@"URL = %@", objectURL);
	NSLog(@"Cache size before = %d bytes", [[NSURLCache sharedURLCache] currentMemoryUsage]);
	NSData *data = nil;
	NSHTTPURLResponse *response = NULL;
	NSError *error = nil;
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:objectURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:20];
	data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
	NSLog(@"Image Request finished. Error = %@",[error localizedDescription]);
	NSLog(@"Cache size after = %d bytes", [[NSURLCache sharedURLCache] currentMemoryUsage]);
        // Cache size = 0 ALWAYS!
    if (error == nil) {
		UIImage *photo = [UIImage imageWithData:data];
		if (photo) {
			[mainViewController performSelectorOnMainThread:callback
												 withObject:[NSArray arrayWithObjects:photo, index, nil] 
											  waitUntilDone:YES];
			[pool release];
			return;	
		}
		
	}
	// ELSE (Error happened)
	[mainViewController performSelectorOnMainThread:callback
										 withObject:[NSArray arrayWithObjects:nil] 
									  waitUntilDone:YES];
	[pool release];
}

Last edited by blueFLoyd8; 01-27-2010 at 11:02 PM.
blueFLoyd8 is offline   Reply With Quote
Reply

Bookmarks

Tags
cache, nsoperation, nsoperationqueue, nsurl

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 354
7 members and 347 guests
.Snipe, BSH, givensur, guusleijsten, NSString, Paul Slocum, SillyHoney
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,645
Threads: 94,111
Posts: 402,862
Top Poster: BrianSlick (7,990)
Welcome to our newest member, leighec68
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:27 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0