Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 118 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I tell if an object has a key value observer attached

#1
If you tell an Objective-C object to `removeObservers:` for a key path and that key path has not been registered, it cracks a sad, like:

> Cannot remove an observer `<observerObject>` for the key path `"theKeyPath"` from `<objectbeingObserved>` because it is not registered as an observer.

Is there a way to determine if an object has a registered observer, so i can do this

```lang-none
if (object has observer){
remove observer
}
else{
go on my merry way
}
```
Reply

#2
The real question is why you don't know whether you're observing it or not.

If you're doing this in the class of the object being observed, stop. Whatever's observing it expects to keep observing it. If you cut off the observer's notifications without its knowledge, expect things to break; more specifically, expect the observer's state to go stale as it doesn't receive updates from the formerly-observed object.

If you're doing this in the observing object's class, simply remember which objects you're observing (or, if you only ever observe one object, whether you're observing it). This is assuming that the observation is dynamic and between two otherwise-unrelated objects; if the observer owns the observed, just add the observer after you create or retain the observed, and remove the observer before you release the observed.

Adding and removing an object as an observer should usually happen in the observer's class, and never in the observed object's.
Reply

#3
The only way to do this is to set a flag when you add an observer.
Reply

#4
Put a try catch around your removeObserver call

@try{
[someObject removeObserver:someObserver forKeyPath:somePath];
}@catch(id anException){
//do nothing, obviously it wasn't attached because an exception was thrown
}
Reply

#5
In my opinion - this works similar to retainCount mechanism. You can't be sure that at the current moment you have your observer.
Even if you check: **self.observationInfo** - you can't know for sure that you will have/won't have observers in future.
<p>Like **retainCount**. Maybe the **observationInfo** method is not exactly that kind of useless, but I only use it in debug purposes.
<p>So as a result - you just have to do it like in memory management. If you added an observer - just remove it when you don't need it. Like using viewWillAppear/viewWillDisappear etc. methods. E.g:

-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self addObserver:nil forKeyPath:@"" options:NSKeyValueObservingOptionNew context:nil];
}

-(void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self removeObserver:nil forKeyPath:@""];
}

<p> And it you need some specific checks - implement your own class that handles an array of observers and use it for your checks.
Reply

#6
I am not a fan of that try catch solution so
what i do most of the time is that i create a subscribe and unsubscribe method for a specific notification inside that class. For example these two methods subcribe or unsubscribe the object to the global keyboard notification:

@interface ObjectA : NSObject
-(void)subscribeToKeyboardNotifications;
-(void)unsubscribeToKeyboardNotifications;
@end

Inside those methods i use a private property which is set to true or false depending on the subscription state like so:

@interface ObjectA()
@property (nonatomic,assign) BOOL subscribedToKeyboardNotification
@end

@implementation

-(void)subscribeToKeyboardNotifications {
if (!self.subscribedToKeyboardNotification) {
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardHide:) name:UIKeyboardWillHideNotification object:nil];
self.subscribedToKeyboardNotification = YES;
}
}

-(void)unsubscribeToKeyboardNotifications {
if (self.subscribedToKeyboardNotification) {
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
self.subscribedToKeyboardNotification = NO;
}
}
@end
Reply

#7
`[someObject observationInfo]` return `nil` if there is no observer.

if ([tableMessage observationInfo] == nil)
{
NSLog(@"add your observer");
}
else
{
NSLog(@"remove your observer");

}
Reply

#8
The whole point of the observer pattern is to allow an observed class to be "sealed" -- to not know or care whether it is being observed. You are explicitly trying to break this pattern.

Why?

The problem you are having is that you are assuming you are being observed when you aren't. This object did not start the observation. If you want your class to have control of this process, then you should consider using the notification center. That way your class has full control on when data can be observed. Hence, it doesn't care who is watching.
Reply

#9
In addition to Adam's answer I would like to suggest to use macro like this

#define SafeRemoveObserver(sender, observer, keyPath) \
@try{\
[sender removeObserver:observer forKeyPath:keyPath];\
}@catch(id anException){\
}

example of usage

- (void)dealloc {
SafeRemoveObserver(someObject, self, somePath);
}

Reply

#10
When you add an observer to an object you could add it to a `NSMutableArray` like this:

- (void)addObservedObject:(id)object {
if (![_observedObjects containsObject:object]) {
[_observedObjects addObject:object];
}
}

If you want to unobserve the objects you can do something like:

for (id object in _observedObjects) {
if ([object isKindOfClass:[MyClass class]]) {
MyClass *myObject = (MyClass *)object;
[self unobserveMethod:myObject];
}
}
[_observedObjects removeAllObjects];

**Remember, if you unobserve a single object remove it from the `_observedObjects` array:**

- (void)removeObservedObject:(id)object {
if ([_observedObjects containsObject:object]) {
[_observedObjects removeObject:object];
}
}
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through