Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUrlConnection in NSThread - No delegate is executed!

I am using an NSURLConnection in an NSThread but none of the NSURLConnection's delegate methods are executed! I have a main method in my NSTread subclass and a while loop that keeps the thread active. Any help?

Sorry for all of this code but I think its the best way to describe my problem. So this is an object that does the async connection calling createConnectionWithPath:userObjectReference

@interface WSDAsyncURLConnection : NSObject 
{
    NSMutableData *receivedData;
    NSDate *connectionTime;
    NSURLConnection *connection;

    id _theUserObject;

}


@property (nonatomic, retain) NSMutableData *receivedData;
@property (nonatomic, retain) NSDate *connectionTime;
@property (nonatomic, assign) NSURLConnection *connection;

- (void)createConnectionWithPath:(NSString *)thePath userObjectReference:(id)userObject;


@end


#import "WSDAsyncURLConnection.h"

@implementation WSDAsyncURLConnection
@synthesize connectionTime, receivedData, connection;


- (void) terminate
{
    if (self.connection) {
        [self.connection release];
        self.connection = nil;
    }
}   


- (void) createConnectionWithPath:(NSString *)thePath userObjectReference:(id)userObject;
{   
    _theUserObject = userObject;


    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:thePath]
                                                cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];


    self.connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];

    if (self.connection) 
    {
        /* record the start time of the connection */
        self.connectionTime = [NSDate date];

        /* create an object to hold the received data */
        self.receivedData = [NSMutableData data];
    } 
}


- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [self.receivedData setLength:0];   
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    /* appends the new data to the received data */ 
    [self.receivedData appendData:data];
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{    
    [self terminate];
}


- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    // displays the elapsed time in milliseconds
    NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:self.connectionTime];
    // displayes the length of data received
    NSUInteger length = [self.receivedData length];

    NSString* aStr = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];   

    [self terminate];


    [[NSNotificationCenter defaultCenter] postNotificationName:WSDAsynchURLConnectionDidFinished
                                                        object:_theUserObject 
                                                      userInfo:[NSDictionary dictionaryWithObject:aStr forKey:@"urlResponseString"]];

    NSLog(@"ti=%f, l=%d, response=%@", elapsedTime, length, aStr);

}

@end

This code is mostly from an apple's example project and it works fine outside an NSThread. But when I use it in the following thread subclass no delegate method is executed !!

@implementation IncomingThread



- (void) main {

    NSAutoreleasePool *poool = [[NSAutoreleasePool alloc] init];


// I start the URLConnection here ... But no delegate is executed !
        [urlConn createConnectionWithPath:@"http://localhost:8888" userObjectReference:nil];


    while (![self isCancelled]) {

        [NSThread sleepForTimeInterval:3.];
    }


    [poool release];

}


- (id) init
{
    self = [super init];
    if (self != nil) {

        urlConn = [[WSDAsyncURLConnection alloc] init];
    }
    return self;
}


- (void) dealloc {

    NSLog(@"deallocating (%@)...", [self className]);


    [urlConn release];

    [super dealloc];
}
like image 738
Vassilis Avatar asked Oct 11 '22 15:10

Vassilis


2 Answers

First of all: you don't need to use NSURLConnection in the separate thread. Since it is asynchronous it doesn't block the main thread. Second: there is not processing of your connection because you always stop the execution of the thread's runloop with this peace of code:

 while (![self isCancelled]) {
        [NSThread sleepForTimeInterval:3.];
    }

From the docs for the sleepForTimeInterval:

No run loop processing occurs while the thread is blocked.
like image 94
Max Avatar answered Oct 17 '22 00:10

Max


You’re doing this the hard way. NSURLConnection does not play very nice with threads, since it needs a run loop to work. Your thread does not have a run loop and therefore the code does not work. Why not run the connection on the main thread? Or you can wrap the connection in an NSOperation, sample code here. And nowadays you also have the option to use a synchronous connection and dispatch it to a global GCD queue.

like image 40
zoul Avatar answered Oct 17 '22 00:10

zoul