NSThread中的NSUrlConnection - 没有执行委托!
我在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];
}
首先:你不需要在单独的线程中使用NSURLConnection。由于它是异步的,它不会阻塞主线程。 二:没有你的连接的处理,因为你总是用这种和平的代码停止线程的runloop的执行:
while (![self isCancelled]) {
[NSThread sleepForTimeInterval:3.];
}
从文档的sleepForTimeInterval
:
No run loop processing occurs while the thread is blocked.
你这样做是艰难的。 NSURLConnection
对线程来说不是很好,因为它需要一个运行循环才能工作。你的线程没有运行循环,因此代码不起作用。为什么不在主线程上运行连接?或者您可以将连接包装在NSOperation
,sample code here中。现在,您也可以选择使用同步连接并将其分派到全球GCD队列。
非常感谢您!我会看看示例代码NSOperation – Vassilis 2011-02-22 18:47:26
你还记得分配代表?
喜欢的东西:
self.connection.delegate = self;
仅仅因为你的WSDAsyncURLConnection
类实现的委托方法,但这并不意味着他们是被调用。
晚,但它可以挽救他人的生命:)
解决方案链接:NSURLConnection的delege方法都不起作用
你是对的!我错过了!谢谢! – Vassilis 2011-02-22 18:44:23
xmmm ...即使我评论`sleepForTimeInterval`仍然没有代表呼叫发生。 (我不释放线程 - 除非在main方法完成时它自己释放,但不是!) – Vassilis 2011-02-22 18:53:52
当然,是的,这会导致线程立即退出。如果你想使用单独的线程,则使用同步请求或在主线程中执行异步请求。 – Max 2011-02-22 18:55:35