NSUrlConnection в NSThread - делегат не выполняется!

Я использую NSURLConnection в NSThread, но ни один из методов делегата NSURLConnection не выполняется! У меня есть основной метод в подклассе NSTread и цикл while, который поддерживает поток в активном состоянии. Любая помощь?

Простите за весь этот код, но я думаю, что это лучший способ описать мою проблему. Итак, это объект, который выполняет асинхронное соединение, вызывающее 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

Этот код в основном взят из примера проекта Apple, и он отлично работает вне NSThread. Но когда я использую его в следующем подклассе потока, метод делегата не выполняется !!

@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];
}
5
задан Vassilis 22 February 2011 в 18:27
поделиться