views:

41

answers:

1

Here's a scenario:

A view controller pushes a new controller to the nav controller. This child controller creates a model that uses a NSURLConnection. When this connection finishes it will make a call like the following:

[self.delegate modelDidFinishParsing:self];

What is the safe way to produce this code? Right now, I have this code and it crashes in a certain situation:

if ([self.delegate conformsToProtocol:@protocol(ModelDelegate)]) [self.delegate modelDidFinishParsing:self];

The situation when it crashes is when the view controller that owns the model is popped from the stack before the model finishes. Should I make the model an ivar so that the controller releases it in its own - (void)dealloc ?

A: 

In your check, you could make sure the delegate isn't nil

if (self.delegate && [self.delegate conformsToProtocol...]) [self.delegate modelDidFinishParsing:self];
Tom Irving
Is that the correct way to do everything?
rickharrison
Well, personally I would use [self.delegate respondsToSelector..] rather than [self.delegate conformsToProtocol..], but other than that, it should work.
Tom Irving