Re: NSURLConnection unhappiness
Re: NSURLConnection unhappiness
- Subject: Re: NSURLConnection unhappiness
- From: "Stephen J. Butler" <email@hidden>
- Date: Thu, 30 Apr 2009 02:09:08 -0500
On Wed, Apr 29, 2009 at 11:31 PM, Nick Hristov
<email@hidden> wrote:
> - (id) initWithURL: (NSURL*) someurl
> {
> self = [super init];
> if (self != nil) {
> self->url = [someurl copy];
> NSURLRequest * request = [NSURLRequest requestWithURL:self->url
> cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 6.0];
> NSLog([NSString stringByAppendingStrings:@"Request: " , [request
> description], nil]);
Ugg. Don't do this, please. NSLog accepts as its first argument a
printf style format string. Also, '%@' gets replaced with [aObject
description]. So all you really need is:
NSLog( @"Request: %@", request );
> self->finished = NO;
> self->connection = [[NSURLConnection alloc]
> initWithRequest:request delegate:self startImmediately:NO];
> }
> return self;
> }
>
> - (void) downloadData
> {
> NSLog([self->connection description]);
Please don't do this either. If [connection description] happens to
contain a '%' then your program will crash. The proper way is to
ALWAYS specify a format string:
NSLog( @"%@", connection );
> NSLog(@"run, b*$#4rd, run...");
> [self->connection scheduleInRunLoop:[NSRunLoop currentRunLoop]
> forMode:NSDefaultRunLoopMode];
> [self->connection start];
> int timeout = 30;
> int i = 0;
> while(self->finished == NO && [self _deletegateTerminateCheck] == NO) {
You probably need to mark 'finished' as volatile in your interface
definition for this to work. Otherwise the assembly generated could
cause your loop to never exit.
> // suspend thread
> [NSThread sleepForTimeInterval: 1.0];
Yeah, as others have said, you need to run the runloop. Otherwise the
connection can't do any work.
> i++;
>
> /// stupid, but effective safety measure:
> if(i >= timeout) {
> break;
> }
> }
> }
>
> - (NSData *) data {
> return self->data;
> }
>
> #pragma mark NSConnectionDelegate implementation
> - (NSURLRequest *)connection:(NSURLConnection *)connection
> willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse
> *)response
> {
> NSLog(@"Will send request");
> return request;
> }
>
>
> - (void)connection:(NSURLConnection *)connection
> didReceiveResponse:(NSURLResponse *)response
> {
> NSLog(@"Did get response");
> NSString * encoding = [response textEncodingName];
> self->dataEncoding = [self getEncodingForEncodingName: encoding];
> self->mimeType = [response MIMEType];
You need to retain the values here.
> }
>
> - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData
> *)receiveddata
> {
> NSLog(@"Did get data");
> if([self _deletegateTerminateCheck]) {
> if(self->data != nil) {
> [self->data release];
> self->data = nil;
> }
> [self->connection cancel];
> [self->connection release];
> self->connection = nil;
> return;
> }
> if(self->data == nil) {
> self->data = [[NSMutableData alloc]init];
> }
> [self->data appendData:receiveddata];
> }
>
> - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
> NSLog(@"did finish loading page");
> self->finished = YES;
> }
>
>
> - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError
> *)error
> {
> NSLog([NSString stringByAppendingStrings:@"Connection failed with error:
> " , [error description], nil]);
Again with the format strings.
NSLog( @"Connection failed with error: %@", error );
> self->finished = YES;
> }
>
> - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
> willCacheResponse:(NSCachedURLResponse *)cachedResponse
> {
> NSLog(@"Will cache response");
> return cachedResponse;
> }
>
> - (void) dealloc {
> if(self->url !=nil) {
> [self->url release];
> self->url = nil;
> }
> if(self->connection != nil) {
> [self->connection release];
> self->connection = nil;
> }
> [super dealloc];
> }
What about releasing the other instance variables you assign?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden