user609906
user609906

Reputation: 190

Check if delegate still exists before calling respondsToSelector

I have made a UIView sub class to deal with iAds and adMob. The view controller is the sub class delegate and everything works ok. Sometimes however, the view controller disappears before the ad has been fetched. To resolve this I set the delegate to nil in my view controller's dealloc implementation.

The problem I have is that sometimes the respondsToSelector: method is ran at the same time as the view controller is getting deallocated. This causes a crash. Does anyone know how I may rearrange this?

The ad view is on about 10 different view controllers so I would like one place to create the ad code.

Many Thanks

Upvotes: 5

Views: 13411

Answers (7)

Stan
Stan

Reputation: 6561

There is actually a fast and not really good solution - to use @try/@catch block. Just if you get to the @catch block your delegate fails for sure... like:

@try{    
     if (delegate && [delegate respondsToSelector:@selector(...)])
     [delegate callBackMethod];
}
@catch (NSException *e){
     // if you get here then delegate is no longer valid regardless its reference is still valid
}

Upvotes: 0

BabyPanda
BabyPanda

Reputation: 1592

Although this is a question long ago, I really messed with it a bit and finally found something that might help.

Set a completion block rather than a delegate for finished or failed event, and this would help.

Upvotes: 2

Matt Connolly
Matt Connolly

Reputation: 9857

If you can't retain the view, then use a static method to get the instance which is cleared in dealloc. ie: instead of:

if (delegate && [delegate respondsToSelector:@selector(...)])

Do this:

if (s_myViewDelegate && [delegate respondsToSelector:@selector(...)])

In the class:

- (id) init {
    s_myViewDelegate = self;
    ...etc...
}

- (void) dealloc {
    s_myViewDelegate = nil;
}

Upvotes: 5

Rahul Vyas
Rahul Vyas

Reputation: 28720

Yes it's a problem with iAd and admob. I had also this kind of problem. I have solved the problem by adding add view on main window and make delegate to app delegate so app delegate will never deallocated until you close the application.

Upvotes: 0

Cocoanetics
Cocoanetics

Reputation: 8247

You should not have 10 individual ad views, that's wrong on so many levels. Instead you should have just one that you either move between individual views or - smarter - just keep on top.

You can for example add a view to a tabBarController.view and this will stay present even if you switch tabs. For views that you don't want the ad on you can simply hide it.

Upvotes: -1

Marcos Crispino
Marcos Crispino

Reputation: 8218

if (delegate && [delegate respondsToSelector:@selector(...)])

Upvotes: 3

futureelite7
futureelite7

Reputation: 11502

One easy way is retaining the view until you had a chance to set the delegate to nil, and then release it. Then you can ensure that the object is still alive and prevent crashes.

Upvotes: 5

Related Questions