Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if there is a delegate listening? [duplicate]

Tags:

objective-c

I have a class that send delegate messages to another class. If the other class is not listening, the app is crash . I was looking for a way to check if the other class is listen before i send the message.

so in the class that post the delegate , i have this condition the check that :

       if ([self.delegate respondsToSelector:@selector(NewDataFromSocketWithString:WithCommand:)])
           [self.delegate NewDataFromSocketWithString:final WithCommand:c];

Where somehow , the condition is always true, even if the class that gets the delegate is already released and not exist anymore .

How do i check the delegate before posting to eliminate crashes ?

like image 671
Curnelious Avatar asked Jan 27 '26 19:01

Curnelious


1 Answers

even if the class that gets the delegate is already released and not exist anymore .

...have you tested that this is the case? If you aren't weak referencing your delegate then this won't be happening. If the delegate has actually been released then self.delegate should return nil, which would mean your if statement would evaluate to false.

The fact your if statement is evaluating to true suggests to me that your delegate is still set. Are you using ARC or manually managing memory? If it's the latter, you'll want your property to be set to assign - if it's the former, you'll want to set your delegate to weak.

like image 93
lxt Avatar answered Jan 30 '26 12:01

lxt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!