Estoy usando un NSURLConnection en un NSThread pero ninguno de los métodos de delegado NSURLConnection se ejecutan! Tengo un método principal en mi subclase NSTread y un ciclo while que mantiene el hilo activo. ¿Alguna ayuda?NSUrlConnection en NSThread - ¡No se ejecuta ningún delegado!
Perdón por todo este código, pero creo que es la mejor manera de describir mi problema. Así que este es un objeto que hace la conexión asincrónica llamando 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
Este código es en su mayoría de proyecto de ejemplo de una manzana y funciona bien fuera de un NSThread. Pero cuando lo uso en la siguiente subclase de subproceso, ¡no se ejecuta ningún método de delegado!
@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];
}
¡Tienes razón! ¡Me lo perdí! ¡Gracias! – Vassilis
xmmm ... Incluso si comento 'sleepForTimeInterval' aún no hay llamadas de delegados. (No lanzo el hilo, excepto si se lanza solo cuando el método 'main' termina pero no!) – Vassilis
Sí, por supuesto, porque el hilo sale inmediatamente. Si desea utilizar un hilo separado, utilice una solicitud sincrónica o haga una asincronía en el hilo principal. – Max