Note that there are some explanatory texts on larger screens.

plurals
  1. PONSUrlConnection in NSThread - No delegate is executed!
    primarykey
    data
    text
    <p>I am using an NSURLConnection in an NSThread but none of the NSURLConnection's delegate methods are executed! I have a main method in my NSTread subclass and a while loop that keeps the thread active. Any help? </p> <p>Sorry for all of this code but I think its the best way to describe my problem. So this is an object that does the async connection calling <code>createConnectionWithPath:userObjectReference</code></p> <pre><code>@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 </code></pre> <p>This code is mostly from an apple's example project and it works fine outside an NSThread. But when I use it in the following thread subclass no delegate method is executed !!</p> <pre><code>@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]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload