I have been scratching my head trying to work out why NSURLConnection is not calling its delegate methods to my class. The method will work fine 99.999% of the time but on the odd occasion the NSURLConnection will be created and then not call back. I have checked the thread with debug information to make sure its performing on the main thread (this function can only be called by pressing a UI element which is always on the main thread anyway). I have also check the number of connections at that time and it is has been 1 on several occasions so I cannot be hitting any form of connection max count. The URL used for this call is the same each time and so I have ruled out an incorrect URL as it works most of the time
The code below shows the function that is called:
- (void) getProperties:(NSString *) userID propertyFlag:(PropertyFlag)propertyFlag delegate:(id) target setState:(id) state
{
if(!userID)
{
NSException *anException = [[NSException alloc] initWithName:NSInvalidArgumentException reason:@"userId is equal to nil" userInfo:nil];
[anException raise];
}
NSDictionary *userState = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:target, state, self, FUNCTION_NAME, nil]
forKeys:[NSArray arrayWithObjects:@"callback", @"state", @"_self", @"request", nil]];
// Create the connection state handler and set the delegates and type
ConnectionStateHandler *connectionStateHandler = [[ConnectionStateHandler alloc] init];
connectionStateHandler.delegate = self;
connectionStateHandler.state = userState;
// create the target URL
NSString *targetURL = [NSString stringWithFormat:@"someurl"];
NSURL *url = [NSURL URLWithString:targetURL];
DLog(@"getProperties:propertyFlag:delegate:setState - url: %@", targetURL);
DLog(@"Is%@ main thread", ([NSThread isMainThread] ? @"" : @" NOT"));
// create the request
NSMutableURLRequest *request = [PSMNSURLRequestBuilder createRequestWithURL:url];
connectionStateHandler.connection = [[[NSURLConnection alloc] initWithRequest:request delegate:connectionStateHandler] autorelease];
}
I have implemented all the required delegate methods and as previously mentioned it works fine normally. If anyone can give me any ideas on what else other than threading, connection count and incorrect URL as to what the problem might be I would greatly appreciate it!
Thanks in advance to any commenters
Edit:
I have implemented the following delegate methods in the ConnectionStateHandler object (each of theses methods have logs in to show when they are called, they are however on the rare occasion not called):
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- (NSURLRequest *)connection: (NSURLConnection *)inConnection willSendRequest:(NSURLRequest *)inRequest redirectResponse: (NSURLResponse *)inRedirectResponse;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- (void)connectionDidFinishLoading:(NSURLConnection *)connection