Is there a way to know if there are objects listening on a keypath for a view controller. For example, I have in my viewcontroller
[tabBarController addObserver:self
forKeyPath:@"selectedViewController"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:&PrivateKVOContext
]; (edited)
And I want to know if my tabbarcontroller can know if my viewcontroller is listening on that keypath
The reason is because my app crashes because the tab bar controller still thinks my view controller is listening on that key path even though my view controller has been deallocated
And in the dealloc method of my viewcontroller, I put [tabBarController removeObserver:self forKeyPath:@"selectedViewController”];
to remove itself as an observer
Sadly not. There's no way to check which objects are currently observing other objects.
As such, it's generally recommended that you place removeObserver:
in the complementing method to the one where you placed addObserver:
so they'll be reliably called an equal number of times.
In this case, since you're putting it in dealloc, which is called at the end of the view controller's lifecycle, you should put addObserver:
in your init
method, which will be called at the very start.
It depends on if the observer really in use, in your code , try this:
- (void)dealloc {
@try {
[[NSNotificationCenter defaultCenter] removeObserver:Notification_Location_Ready];
} @catch (NSException *exception) {
} @finally {
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.