On Jun 21, 2011, at 07:16, talesp wrote:

> 1.: should I just check if the object is different from self? Or some more 
> "complicated"? And what should I check as keyPath?

In general, different classes can use the same property names, and keyPath 
refers to a property name. So, you need to be certain you're recognizing the 
*right* keyPath.

If you've already checked the context parameter (to eliminate observations that 
you didn't set up), then you might do one of these things:

a. If you're *certain* this observer is not observing the same keyPath for 
objects of a different class, then you don't need to check the object. The 
danger here is if you go back and add more observations later, you might cause 
yourself some annoying bugs.

b. Or, you can just check the class of the object ('isKindOfClass:'), if you 
don't care which object it is.

c. Or, you can check that the object is the one you are observing (==), if 
that's easier or if you need to do different things for each observed object.

> 2.: if I send a context parameter from observer (the context can be a 
> NSString for example, right?), should I chech if the string is expected? And 
> I only should call super if the context is invalid? Reading Apple 
> documentation I saw that I should ever call super, but when I did it, the app 
> crashed (I did not went look what was wrong. Should I or it's not really 
> necessary?)

Yes, a string is fine, and you should only call super if you do not recognize 
the string. (If everyone played by the rules, it wouldn't matter if you called 
super every time, but you can assume there's plenty of code out there that 
doesn't check the context properly, perhaps even in the Cocoa frameworks.)

Just to clarify: Every KVO notification arriving at 
'observeValue:forKeyPath:...' is the result of *one* 
'addObserver:forKeyPath:...', and should be processed only once. If it's your 
notification, process it and return. If it's not, call super to give someone 
else a chance to process it.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to