The error handling code fails (go figure)


#1

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [connectionInProgress release]; connectionInProgress = nil; [xmlData release]; xmlData = nil; NSString *errorString = [NSString stringWithFormat:@"Fetch failed: %@", [error localizedDescription]]; UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:errorString delegate:nil cancelButtonTitle:@"OK" destructiveButtonTitle:nil otherButtonTitles:nil]; [actionSheet showInView:[[self view] window]]; [actionSheet autorelease]; }

raises the following exception when I turn the wifi off:

2010-07-13 16:07:29.591 TopSongs[81:207] *** Assertion failure in -[UIActionSheet showInView:], /SourceCache/UIKit/UIKit-1145.66/UIAlert.m:7073
2010-07-13 16:07:29.609 TopSongs[81:207] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Invalid parameter not satisfying: view != nil’

Any ideas what could be going wrong?


#2

Hi,

I can’t see anything wrong with the snippet but the message seems to be saying that [self view] is nil.

Have you got an extra release somewhere ?

Gareth


#3
So I've poked around in the dev docs a bit, and since I'm using an iPad rather than an iPhone, it seems that the Cancel button isn't used. (It says to specify "NIL" if you don't want one or are using an iPad.) The proper code for iPad implementation is [actionSheet showInView:[self view]]; as opposed to the way it is written in the book. I don't know if this is an iPad thing, an IOS4 thing, or a mistake in the book. The lack of reader comments regarding this leads me to believe it's an iPad/IOS4 thing.

I’m running into this as well; and not sure why.

I added an extra step into the code for debugging:

UIView *v = [self view];

then I made this modification:

//[actionSheet showInView:[[self view] window]];

[actionSheet showInView:v];

It worked (without putting an OK button on the screen, but tapping the screen made the notification go away)

of course if I change that line to:

[actionSheet showInView:[v window]];

It throws the exception again. So it would seem that the problem is the window property is nil. Since I’ve poured through the code as entered in the book, and I haven’t found any discrepancies with my typing it in, I’m wondering if this is an iPad thing or is there something else going on?


#4

Hi,

I’m pretty sure it’s a timing issue where the view hasn’t been fully wired in to the window at the point the action sheet is trying to display.

Using the simulator (iPad and iPhone) I tried the following:

Turn off wifi - worked OK
Change the url to a non-existent url - worked OK
Change the url to badly formed one - worked OK

I then manually forced the error method in loadSongs
connectionInProgress=[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
[self connection:connectionInProgress didFailWithError:nil];

and this triggered the view = nil assertion failure - aha

So I then did the same thing but in didReceiveData and it was back to being OK again.

final test was to move the [self loadSongs] out of loadView and into viewDidLoad and again it worked OK.

hence my thinking.

HTH
Gareth