1

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
codercat
  • 22,873
  • 9
  • 61
  • 85
  • I don't get it. You set a local variable (connectionStateHandler) as delegate for an NSURLConnection. – Larme Jan 21 '14 at 17:02
  • The connectionStateHandler object is not autoreleased and so is not auto released until the connection has finished its delegate callbacks to it. After the NSURLConnection has finished its delegate callbacks (success or failure) the connectionStateHandler calls back to where it needs to and is released in that function (I am aware its not an overly nice way of releasing the object) – user2067925 Jan 21 '14 at 17:08
  • Have you implemented both `connection:didFailWithError:` and `connection:didReceiveResponse:` in your delegate? You may not see your `connection:didReceiveData:` called if either `didFailWithError` reports an error or, sometimes, if `didReceiveResponse:` reports a `statusCode` that is not 200. – Rob Jan 21 '14 at 17:16
  • Hi Rob, I have edited my question to show the methods I have, none are called on the rare occasion when my problem occurs – user2067925 Jan 21 '14 at 17:21
  • What timeout value are you using? How do you know on those rare occasions its not being called its because the server isn't responding but you're not waiting long enough to see if the timeout limit is being reached? – Gruntcakes Jan 21 '14 at 17:25
  • The timeout limit is set at 30 seconds, but even after this time no method is called. As I am fortunate enough to be in the same room as the server I am calling I can see that the connection is never made to it – user2067925 Jan 21 '14 at 17:27
  • So the problem is not that your delegates aren't being called, but that sometimes the request is not making it through to your server? Which is an entirely different issue. Does the delegate get called after the timeout value at least? – Gruntcakes Jan 21 '14 at 17:34
  • No delegate is called at any point, it just hangs even after the timeout period. Which is why i believe it is just not starting the request (I have tried using the -start function and it makes no difference) – user2067925 Jan 21 '14 at 17:36
  • I've used NSURLConnection extensively in multiple different projects and have not noticed this. So don't know what to suggest other than perhaps there could be a larger architectural issue with your program that cannot be seen from this narrow window of code. – Gruntcakes Jan 21 '14 at 17:50

0 Answers0