Hello,

I am currently working on a small iphone app, and I am facing some
difficulty with getting NSURLConnection to ... connect. Basically my unit
tests don't pass: connection is never made.

Here is a snippet of the non-working code:

<code>

- (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]);
        self->finished = NO;
        self->connection = [[NSURLConnection alloc]
initWithRequest:request    delegate:self startImmediately:NO];
    }
    return self;
}

- (void) downloadData
{
    NSLog([self->connection description]);
    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) {
        // suspend thread
        [NSThread sleepForTimeInterval: 1.0];
        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];
}

- (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]);
    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];
}
</code>

Here is the output that I get from the build:

2009-04-25 14:14:22.699 otest[6135:80f] Request: <NSURLRequest
http://arstechnica.com>
2009-04-25 14:14:22.704 otest[6135:80f] <NSURLConnection: 0x31d2a0,
http://arstechnica.com>
2009-04-25 14:14:22.705 otest[6135:80f] run, b*$#4rd, run...
2009-04-25 14:14:22.706 otest[6135:80f] hello?
2009-04-25 14:14:52.709 otest[6135:80f] Data is nil!

"Data is nil!" is dumped by the

[NSString stringByAppendingStrings: .... ] is my own extension and it works.

I also found
http://www.cocoabuilder.com/archive/message/cocoa/2009/3/31/233409 which
helped, without scheduling in the run loop things fail miserably.

Once again, this is all run inside a unit test. So I am not sure what else
may be missing from the run loop, looks like the URL connection never really
even starts the loading mechanism.

Thanks a bunch,

Nick
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to