Re: KVO Question: How to programmatically determine if one object is observing another on a keyPath and context?
Re: KVO Question: How to programmatically determine if one object is observing another on a keyPath and context?
- Subject: Re: KVO Question: How to programmatically determine if one object is observing another on a keyPath and context?
- From: Richard Somers <email@hidden>
- Date: Fri, 28 Sep 2012 10:04:58 -0600
On Sep 11, 2012, at 10:06 AM, Motti Shneor <email@hidden> wrote:
> OK. Could you spare some pseudo-code for this? Or some link to a known code-sample? or maybe some 10 highlight lines from your own utilities?
>
> Documentation on method swizzling isn't that easy to use. In my case, all I need to do is to "swizzle?" (like override) the "addObserver" of my observed object, and record the high-level observation info (observer, options, context, etc.) in some static container, so I can later question the observed object about its observer.
>
> But how do I do that on my NSManagedObject sublcass?
Sorry about the delayed response. I have been buried in work. I switched to 10.8 Mountain Lion and I am still adjusting to the revised Mail applicaiton. You could do swizzling like this.
#import "JRSwizzle.h"
@implementation NSManagedObject (MySpecialCategory)
// Do the swizzling
+ (void)load
{
@autoreleasepool // NSAssert requires an autorelease pool
{
NSError *error = nil;
[[NSObject class] jr_swizzleMethod:@selector(method)
withMethod:@selector(my_swizzled_method)
error:&error];
NSAssert(error == nil, @"%@", error);
}
}
// Framework method
// - (void)method
// {
// ...
// }
// Swizzled method
- (void)my_swizzled_method
{
// do your thing
// call original implementation
[self my_swizzled_method];
}
@end
Note that the NSManagedObject documentation indicates "As with any class, you are strongly discouraged from overriding the key-value observing methods". I view swizzling like this: You are at your wits end, you have examined all other options, you want to use Apple's frameworks but you simply can figure out any other way. So you try swizzling.
--Richard Somers
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden