我在NSThread中使用NSURLConnection,但没有执行NSURLConnection的委托方法!我的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之外工作正常。 但是当我在下面的线程子类中使用它时,没有执行委托方法!!
@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];
}
答案 0 :(得分:3)
首先:您不需要在单独的线程中使用NSURLConnection。由于它是异步的,因此不会阻塞主线程。 第二:没有处理你的连接,因为你总是用这段代码停止执行线程的runloop:
while (![self isCancelled]) {
[NSThread sleepForTimeInterval:3.];
}
来自sleepForTimeInterval
的文档:
No run loop processing occurs while the thread is blocked.
答案 1 :(得分:1)
你这么做很难。 NSURLConnection
对线程不起作用,因为它需要一个运行循环才能工作。您的线程没有运行循环,因此代码不起作用。为什么不在主线程上运行连接?或者,您可以将连接包装在NSOperation
,sample code here中。现在,您还可以选择使用同步连接并将其分配给全局GCD队列。
答案 2 :(得分:0)
你还记得分配代表吗?
类似的东西:
self.connection.delegate = self;
仅仅因为你的类WSDAsyncURLConnection
实现了委托方法,并不意味着它们被调用。
答案 3 :(得分:0)
迟到但可能会挽救他人生命:)
解决方案链接是:NSURLConnection删除方法不起作用