objective-c - NSThread 中的 NSUrlConnection - 没有执行委托(delegate)!

标签 objective-c url connection nsthread delegation

我在 NSThread 中使用 NSURLConnection,但没有执行 NSURLConnection 的委托(delegate)方法!我的 NSTread 子类中有一个 main 方法和一个保持线程事件的 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

此代码主要来自苹果的示例项目,它在 NSThread 之外工作正常。 但是当我在下面的线程子类中使用它时,没有执行委托(delegate)方法!!

@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];
}

最佳答案

首先:您不需要在单独的线程中使用 NSURLConnection。因为它是异步的,所以它不会阻塞主线程。 第二:没有处理你的连接,因为你总是用这段代码停止线程运行循环的执行:

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

来自sleepForTimeInterval 的文档:

No run loop processing occurs while the thread is blocked.

关于objective-c - NSThread 中的 NSUrlConnection - 没有执行委托(delegate)!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5082166/

相关文章:

ios - Delphi XE4 iOS无法连接PAServer

iOS 6 - UIBarButtonItem setStyle 不工作

ios - 如何在 objective-c 中将 NSString 转换为 NSDictionary?

python - ImageField 的 url 方法返回非 Url 值 - Django

javascript - 互联网浏览器 : HTML entities in URL

grails - Groovy SQL 数据源连接处理,withTransaction

objective-c - 如何将依赖项注入(inject) iOS View Controller ?

ios - 如何 "simplify"UIImagePickerController

Python从互联网地址下载所有文件?

java - 在 Android 5+ 中将应用程序设置为使用蜂窝网络而不是 WiFi