Tuesday, June 22, 2010

Different behaviour in iOS4 for NSError and NSURLConnection

After running some of our apps now on iOS4 we were surprised that there were much more crashes. This little code section is what caused the crashes:

NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:requestStr]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSURLResponse *response;
NSError *error;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(error) {
NSLog(@"error: %@", [error localizedDescription]);
}

After hours of debugging we could narrow it down to the line of code that initialises the NSError. Setting it to nil did remove the crash. So make sure your code looks like this:

NSError *error = nil;

We are currently investigating more issues we have with code that ran perfectly fine in 3.x and now starts crashing with iOS4. The above line fixed approximately 80% of our problems.

Good luck making your apps ready for iOS4.

5 comments:

Peter said...

Thank you very much, you are a hero! I had the same, I tried to find the reason with debugging, but even the xcode debugger died because of the missing initialization of NSError variable.

ghaygood said...

Thanks for posting this! I've been beating my head up for 2 hours on this one....

Chuck said...

This saved me quite a lot of time, Thanks so much!

anAlyst said...

Am using Titanium SDK. Found similar code in Classes/ASI/ASIHTTPRequest.h

However, initialising it to nill simply broke up the compile :!

Any help?

regards
Anshu Prateek

Android app development said...

This is one of the knowledgeable and good post.I like your blog talent.Thanks for your support.
Android app developers