So for the past two days I’ve had my iPhone app crashing and vomiting “EXC_BAD_ACCESS” in the console. Generally speaking, this error means you’ve released a reference to some object you still need and some other object tried to send a message to said object afterward. It turns out that it was entirely my fault and (GASP) paying a little more attention to Apple’s reference would saved hours and hours of debugging for me.
NSURLConnection is actually pretty simple to use but here are some tips:
- Use the asynchronous mode for any reasonably large file (bigger than a few KB) as people might be on a slow Edge network connection and locking up the view is never a good thing.
- Do not send the message “start” to your connection if you use connectionWithRequest or initWithRequest to initialize your connection as you’ll get the nasty “EXC_BAD_ACCESS” error mentioned above. The reason for this is simple: You’ve scheduled your connection to time out in n seconds, the method that initializes the connection starts fetching data immediately. You then send the message “start,” which spins off the connection trying to fetch data again. This works fine except that the original attempt eventually times out and tries to call back, hosing everything over and crashing your application hard core. Oops. You should only send the message “start” to your connection if you initialize it with initWithRequest:delegate:startImmediately.
- When setting up NSURLRequest to pass to NSURLConnection, I highly recommend using NSMutableURLRequest so you can set the User-Agent header. Some http servers may not return data, even for a simple GET without the User-Agent. It’s my understanding that User-Agents are optional, but I do not know if said server behavior is nonstandard.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: @"http://myurl.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
//Borrow FF2’s User-Agent
[request setValue: @"Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16" forHTTPHeaderField: @"User-Agent"];
I hope this saves someone some time.
How I Roll