Arouri
Arouri

Reputation: 48

Using assign with delegate

I have a download manager that asynchronously calls a delegate method of the viewController when the data is ready, but if the user press back (the viewController is removed and deallocated), the download manager tries to send a message to undefined delegate. To avoid this, I'm trying to check the delegate when the data is ready, to get sure that it is not nil and responds to the selector am trying to call. But an exception is raised when I try to check if it is nil or not.

I defined the delegate with an assign property not retain.

How I should check to avoid delegating a not defined controller ?

Thanks

Upvotes: 2

Views: 132

Answers (2)

Tomasz Wojtkowiak
Tomasz Wojtkowiak

Reputation: 4920

It seems to me that the best solution would be do not use delegate, but send a notification using NSNotificationCenter.
Your viewController should have defined observer for this notification.

Upvotes: 0

Till
Till

Reputation: 27597

Sounds as if you have implemented the delegate the wrong way around.

Your download manager should hold an instance variable named e.g. delegate. After or while initializing the download manager, the viewController sets itself as the delegate for that download manager - e.g. downloadManager.delegate = self;

Your viewController should nil that exact value once it unloads/deallocs - somewhat like `

- (void)dealloc
{
    downloadManager.delegate = nil;
    ...
    [super dealloc];
}

Now the download manager may check its own instance variable (delegate) for non nil etc without any problems.

Upvotes: 3

Related Questions