gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:52:57 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com