2011-02-22 68 views
5

我在NSThread中使用NSURLConnection,但沒有執行NSURLConnection的委託方法!我在我的NSTread子類中有一個主要方法,並有一個可以使線程保持活動狀態的while循環。任何幫助?NSThread中的NSUrlConnection - 沒有執行委託!

對不起,我認爲這是描述我的問題的最好方法。因此,這是確實大多來自蘋果的示例項目的異步連接,因此調用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 

此代碼是一個對象,它的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]; 
} 

回答

2

首先:你不需要在單獨的線程中使用NSURLConnection。由於它是異步的,它不會阻塞主線程。 二:沒​​有你的連接的處理,因爲你總是用這種和平的代碼停止線程的runloop的執行:

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

從文檔的sleepForTimeInterval

No run loop processing occurs while the thread is blocked. 
+0

你是對的!我錯過了!謝謝! – Vassilis 2011-02-22 18:44:23

+0

xmmm ...即使我評論`sleepForTimeInterval`仍然沒有代表呼叫發生。 (我不釋放線程 - 除非在main方法完成時它自己釋放,但不是!) – Vassilis 2011-02-22 18:53:52

+0

當然,是的,這會導致線程立即退出。如果你想使用單獨的線程,則使用同步請求或在主線程中執行異步請求。 – Max 2011-02-22 18:55:35

1

你這樣做是艱難的。 NSURLConnection對線程來說不是很好,因爲它需要一個運行循環才能工作。你的線程沒有運行循環,因此代碼不起作用。爲什麼不在主線程上運行連接?或者您可以將連接包裝在NSOperationsample code here中。現在,您也可以選擇使用同步連接並將其分派到全球GCD隊列。

+0

非常感謝您!我會看看示例代碼NSOperation – Vassilis 2011-02-22 18:47:26

0

你還記得分配代表?

喜歡的東西:

self.connection.delegate = self; 

僅僅因爲你的WSDAsyncURLConnection類實現的委託方法,但這並不意味着他們是被調用。

0

晚,但它可以挽救他人的生命:)

解決方案鏈接:NSURLConnection的delege方法都不起作用