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

emergency preparedness supplies

Hope to read more excellent post from you... emergency preparedness supplies

by Unregistered Visitor on 04/13/15

Paket Liburan Jogja

This is a really good post this. Truly, a magnificent art is making an article. And you are quite a master at that art. I would tell you to keep on continuing this awesome work of yours Paket Wisata Goa Pindul. Paket Pelancongan Yogyakarta.

by Unregistered Visitor on 04/13/15

fast cash loan

I would like to return the favor and that's why I am writing this positive comment for your article.fast cash loan

by Unregistered Visitor on 04/13/15

franklin7

This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you. live tv

by Unregistered Visitor on 04/14/15

franklin7

This type of message always inspiring and I prefer to read quality content, www.trueplr.com

by Unregistered Visitor on 04/14/15

alix

I am continually amazed by the amount of information available on this subject. What you presented was well researched and well worded in order to get your stand on this across to all your readers. Alfombras kilim

by Unregistered Visitor on 04/14/15

I really

I think this is one of the most significant information for me. And i’m glad reading your article. But should remark on some general things, The web site style is perfect, the articles is really great : D. Good job, cheers samsung laptop

by Unregistered Visitor on 04/14/15

franklin7

You give readers a lot to think about and I appreciate that kind of writing. Inbox Inner Circle Review

by Unregistered Visitor on 04/15/15

ambercal

Every few minutes Firefox tries to open a site. Because I just got a Trojan off my computer. So the link to the virus does not work anymore but Firefox keeps trying to open it. It says it cannot display this webpage. So how do I stop this?. www.blog-sardinefactory.com |

by Unregistered Visitor on 04/15/15

Movers and Packers Hyderabad

Packers and Shifting companies Expenses & Rates Just Visit for Best info: Movers and Packers Hyderabad Movers and Packers Bangalore Movers and Packers Mumbai

by Unregistered Visitor on 04/15/15

Movers and Packers Chennai

Thanks for post this helpful post - Please visit for More information about - Movers and Packers Chennai Movers and Packers Delhi Movers and Packers Gurgaon Movers and Packers Pune

by Unregistered Visitor on 04/15/15

Packers and Movers Noida

This article is really fantastic and thanks for sharing the valuable post. Packers and Movers Noida Packers and Movers Thane Packers and Movers Navi Mumbai Packers and Movers Amritsar Packers and Movers Jalandhar Packers and Movers Ghaziabad Packers and Movers Faridabad

by Unregistered Visitor on 04/15/15

Smart Packers and Movers India

This article is really fantastic and thanks for sharing the valuable post. Packers and Movers in Hyderabad Packers and Movers in Bangalore Packers and Movers in Delhi Packers and Movers in Thane

by Raghuraj kumar on 04/15/15

Smart Packers and Movers India

If you are looking Best Packers and Movers so Just Visit At: Packers and Movers in Chennai Packers and Movers in Pune Packers and Movers in Gurgaon Packers and Movers in Mumbai Packers and Movers in Noida Packers and Movers in Navi Mumbai

by Raghuraj kumar on 04/15/15

Packers and Movers India

This article is really contains lot more information about This Topic. - Packers and Movers in Noida Packers and Movers in Ghaziabad Packers and Movers in Faridabad

by Raghuraj kumar on 04/15/15

m very enjoy

I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy. web directory

by jdggf dffdf on 04/16/15

wise foods

This is a very good post which I really enjoyed reading. wise foods Thanks for the reliable information you have shared to us!

by Unregistered Visitor on 04/17/15

Tech Audible

nlike 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). ShowBox for Android

by Unregistered Visitor on 04/17/15

Cara Menjadi Member Green World

The bath fluid never contacts atmospheric conditions thus eliminating the possibility of ice formation. The combination of a small internal bath volume, Cara Menjadi Member Green World strong cooling capacity and a powerful pump enable quick responses to external events (exotherms) and fast pre-programmed temperature profiles.

by Unregistered Visitor on 04/17/15

alix

Im no expert, but I believe you just made an excellent point. You certainly fully understand what youre speaking about, and I can truly get behind that. web

by Unregistered Visitor on 04/18/15

ariasystems

Really appreciate this wonderful post that you have provided for us Cloud Billing Systems Thank you very much.

by Unregistered Visitor on 04/19/15

Thanks

Thanks for this clear and easy to understand instructions... read this

by Unregistered Visitor on 04/19/15

Cheats

pou Make the most of mainly premium substances - you will find him or her for: hill climb racing

by ahmed raza on 04/19/15

Add comment