Begin main content

PMPKVObservation – Yet Another KVO-improvement project

If you’re reading this then you’re probably as frustrated by seemingly random KVO crashes and/or the pain of huge if/else blocks in your observers. I’ve never been happy with the other KVO-improvement classes I’ve seen, and never having had any bright insights myself I kept doing things the normal way. This becomes especially painful with things like view based NSTableViews when you are re-using objects and so need to observe a new object, being very careful to un-observe the prior object (unless it’s been released, which you need to either track yourself if you can, or retain it, which has its own problems).


It was clear that the dealloc swizzling approach of Mike Ash’s MAKVONotificationCenter was unavoidable, but I didn’t like the complexity. Recently Gwynne Raskind posted a somewhat updated MAKVONotificationCenter which sparked some discussion, including a comment discussing SPKVONotificationCenter by Joachim Bengtsson. Joachim’s brainwave was that observations should be modelled as standalone objects and simply managing the lifecycle of that object appropriately should remove the observation. Clean and simple.

Except it’s not quite that simple because you still need to swizzle the dealloc of the observed object since it can go away at any time. And as much as I love a good macro as much as the next hacker, Joachim’s $depends() macro looks about as much fun as a turing complete makefile.

Enter PMPKVObservation!

Include PMPKVObservation.m and .h in your project, then do something like:

#import"PMPKVObservation.h"

@interface PMPKVOTests ()
@property (retain) PMPKVObservation * kvo;
@end

@implementation PMPKVOTests
@synthesizekvo = _kvo;

- (void)observationFired:(PMPKVObservation *)observationchanges:(NSDictionary *)changes;
{
NSLog(@"observation fired for object: %@ keyPath: %@ changes: %@",
observation.observee,
observation.keyPath,
changes);
}

- (void)selectorObservation
{
self.kvo = [PMPKVObservation observe:anObjectToObserve
observer:self
selector:@selector(observationFired:changes:)
keyPath:@"observeMe"
options:0];
}

- (void)blockObservation
{
self.kvo = [PMPKVObservation observe:anObjectToObserve
keyPath:@"observeMe"
options:0
callback:^(PMPKVObservation *observation, NSDictionary *changeDictionary) {
NSLog(@"observation fired for object: %@ keyPath: %@ changes: %@",
observation.observee,
observation.keyPath,
changes);
}];
}

- (void)dealloc
{
[ _kvo release];

[super dealloc];
}

@end

The options and change dictionary are as per normal KVO. You need to be wary of the block causing retain cycles of self as usual.

Unlike MAKVONotificationCenter and others, you are responsible for managing the lifecycle of the kvo object – when it is dealloc-ed the observation will be removed. Like MAKVONotificationCenter, a minimum of magic is used to swizzle the dealloc method of the observed object and remove the observation if it still exists (tracked via an associated NSHashTable). If for some reason you want to find out if the kvo object you hold has been released early you can check the isValid accessor (which you can observe with KVO).

Caveats

  1. While I am using this in a client project, that project has not yet shipped so I can’t promise wide-spread testing. Take a look at the tests in the test app and if you are using scenarios that you don’t thing are tested pull requests are welcome!
  2. I haven’t tried this under ARC, but I think I’ve made PMPKVObservation.m so it will compile and work as-is (the same can’t be said for the test app). Please let me know how you go.
  3. No attempt is made to stop you changing the observed object etc. after creation, but doing so isn’t going to be very effective…

Repository: https://github.com/aufflick/PMPKVObservation

06:49 PM, 13 Mar 2012 by Mark Aufflick Permalink

Good work keep it up

Need some more explanation with brief workouts and also if you need high quality articles then you can http://american-writers.org/ try this site.

by Unregistered Visitor on 08/06/14

Nice oNe

I had to write entire four pages of nonsense about the Key Value Observation when I was in college. I am so surprised by your program actually. It seems to be very interesting. I want to try this program and see how this thing turns out… fashion for play

by Unregistered Visitor on 08/13/14

Passbook passes online

Not worried about our mobile apps. Last days age my friends become in trouble. All of them the apps of mobile are destroyer.Then i am suggest our friends Passbook passes online this website. Now all of my friends are Happy.

by Unregistered Visitor on 08/26/14

Well Done

It is perfect time to make some plans for the future and it is time to be happy. I've read this post and if I could I desire to suggest you some interesting things or suggestions. the venus factor diet

by Unregistered Visitor on 09/05/14

Increase Facebook likes

Inorder to increase fb likes, you should follow some steps to increase likes in facebook. Without the help of these, you can't boost your like. The trick is powered by firstandgeek.com

by Unregistered Visitor on 09/10/14

manag

Joachim’s brainwave seemed to be that findings need to be modelled while separate materials and just managing the lifecycle of this item adequately must eliminate the observation. essaystate.com

by Unregistered Visitor on 09/11/14

These are really

These are really a superb showcases here I am glad to have a review of it and feel to roll on its steps for my benefit always
Mobile News Gathering Solutions and Mobile broadcasting in india

by Unregistered Visitor on 09/11/14

xsui

Very nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. Buy Home Decor Online

by Unregistered Visitor on 09/11/14

Add comment