I want to detect a timeout, but every time my app reaches the line to check for it, it crashes whether there was an error or not. Am I fundamentally not understanding how NSError works? Should I be implementing NSURLConnection with a delegate instead?
I've looked into this for several hours and can't make any headway and I'm pretty sure I'm just going about it in completely the wrong way. Any help in moving in the right direction would be greatly appreciated. Thanks in advance!
Code:
NSError *error;
NSURLRequest *ccRequest =
[NSURLRequest requestWithURL:[NSURL
URLWithString:[self createUrl]]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLResponse* response;
NSData* myData = [NSURLConnection sendSynchronousRequest:ccRequest
returningResponse:&response
error:&error];
// 1001 is the error code for a connection timeout
// It CRASHES below when it tries to determine the error code.
if ( error && [error code] == 1001 ) {
NSLog( @"Server timeout!" );
}
When the app crashes, and you open the debugger, type the word "backtrace" or just "bt" and it should spit out some more info about the crash
Weird. That does absolutely nothing. Anything, I've since learned that synchronous NSURLConnection is buggy and I should use asynchronous instead. Anyway, thanks for the tip! I'm sure it will come in handy on future projects.