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

код тн вэд

Advantageously, the article is really the best on this notable topic. I harmonize with your conclusions and will thirstily look forward to your approaching updates код тн вэд

by Unregistered Visitor on 10/21/14

Nice post

This post is very helpful for me. I learned a lot. Thanks for sharing. my web

by Unregistered Visitor on 10/22/14

works

It is a building a web page, functionality first prior to the interface. It is more important that this program How apple pay works with passbook? works in lieu of beauitifying it without having functionality. Great post!

by Unregistered Visitor on 10/24/14

rock

Really yielding trifling undivided school terminate up soul requiring a dispatch on the thread attendant in precept to uproot despite an appropriate retain slightly moreover middle comprehends in directive to obsolesce later. car insurance

by Unregistered Visitor on 10/27/14

jack

backlinks

by Unregistered Visitor on 10/27/14

rock

Your latest tell gives drag mass support on where to explore a fabulous bandy musician that might be tried stationary useful. crowdfunding software

by Unregistered Visitor on 10/31/14

jogos de moto

i really liked this a lot novos jogos de moto

by Unregistered Visitor on 10/31/14

rock

My husband too i gain also been knotty to full my possess apparatus to receive a outlaw in adjunct My occupation is onto operating unaccustomed fellow of sundry. Made In Japan

by Unregistered Visitor on 11/01/14

great

well wow this was really amazing guys low calorie recipes

by Unregistered Visitor on 11/02/14

WordPress restaurant themes

Hi buddy, your blog's design is simple and clean and i like it. Your blog posts are superb. Please keep them coming. Greets!!! WordPress restaurant themes

by Unregistered Visitor on 11/06/14

zeeshan1

I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job! http://binary-options-reviews.com/text.php?lang=heb&id=9&sid=28

by ahmed raza on 11/07/14

jackson

This is my initial span i stopover here. I discovered so numerous entertaining force in your blog, essentially its palaver. From the tons of notes on your doctrines, I shot I am nay the except individual holding total the idle here! Fulfill up the amazing process. FreeDebtConsolidationQuotes.com

by Unregistered Visitor on 11/09/14

jack

I would also motivate just about wisdom teeth removal lauderhill every person to save this web page for any favorite assistance to assist posted the appearance.

by Unregistered Visitor on 11/10/14

Nice Post

I was searching this type of information from a week. Now I am happy to read it. It Solve may problem. rubber stable mats for sale

by Unregistered Visitor on 11/12/14

Good One

Simply great work. I appreciate the efforts that you make spreading this information. ground reinforcement matting

by Unregistered Visitor on 11/12/14

I like It

I discovered this particular write-up being really educational. Many thanks pertaining to widening my personal understanding of this particular issue. Without doubt its an excellent write-up likewise. Thanks. gymnasticmatsco

by Unregistered Visitor on 11/12/14

Really Amazing

This subject has interested me for quite some time. I have just started researching it on the Internet and found your post to be informative. Thanks. cheap garage flooring tiles

by Unregistered Visitor on 11/12/14

use

This blog is very useful information for me and guidance. This is a fantastic alternative to medical and chemical treatments. Thanks for sharing this information here. charbon chicha

by Unregistered Visitor on 11/13/14

raido

Year 2013 has been quite eventful. Broadcast consultancy

by Unregistered Visitor on 11/14/14

Emulatore 3DS per PC

Nintendo ha tenuto un flusso dal vivo ieri sui prossimi giochi per la console Nintendo 3DS, Nintendo Wii e 2DS U. Anche se, Nintendo ha annunciato ulterior Emulatore 3DS per PC informazioni Amiibo per il Wii U la chiara attenzione si è concentrata sui titoli per Nintendo 3DS e Nintendo 2DS.

by Unregistered Visitor on 11/14/14

Emulatore 3DS per PC

A volte, la vita di portare giù. In generale, si potrebbe andare a meraviglia, ma poi un paio di cose che succedono e il giorno si scende. Le cose non stanno Emulatore 3DS per PC andando la tua strada, e tutto solo si sente fuori. Potrebbe essere solo ti sei svegliato dalla parte sbagliata del letto: ...

by Unregistered Visitor on 11/14/14

zeeshan

Awesome material! Thanks a lot, dude, for giving this so good material to us. I've just recently completed one of my projects due to the information from this post. Thanks man, once again! http://www.fitness-products-reviews.com

by ahmed raza on 11/15/14

Sutopo

thanks nice post homestay Kursus SEO Toko Komputer Kursus Bahasa Inggris

by Unregistered Visitor on 11/15/14

Sasuke

thanks nice post rental mobil solo sewa mobil jogja sewa bus yogyakarta/ rental mobil jogja

by Unregistered Visitor on 11/15/14

joss

thanks nice post Setia Furqon hotel di jogja

by Unregistered Visitor on 11/15/14

oxygen miracle cure reviews

A reliable blog always comes-up with new and exciting information and while reading I have experience that this blog is really have all those quality that qualify a blog to be a good one oxygen miracle cure reviews

by Unregistered Visitor on 11/22/14

zeeshan

Phoenix Tile Cleaning Extraordinary, This particular net page is genuinely exciting and delight to learn. I'm a colossal fan from the subjects specified. Surprise Tile Cleaning

by ahmed raza on 11/23/14

Artemis Hospital

Artemis Hospital is the first JCI and NABH accredited hospital in Gurgaon.
Hospital in gurgaon
Liver transplant in gurgaon

by Unregistered Visitor on 11/27/14

rozi

The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.Natural Vitiligo Treatment System

by Unregistered Visitor on 11/27/14

subjeasdasdst

Thanks for the valuable information and insights you have so provided here... veneers lauderhill

by Unregistered Visitor on 11/28/14

nyc home inspectors

I read this article. I think You put a great deal of exertion to make this article. I like your work. nyc home inspectors

by ahmed raza on 11/28/14

rozi

This is also a very good post which I really enjoyed reading. It is not every day that I have the possibility to see something like this.. houston real estate listings

by Unregistered Visitor on 11/29/14

rebelmouse

I should say only that its awesome! The blog is informational and always produce amazing things. https://www.rebelmouse.com/reviewsgrowtaller4idiots/

by ahmed raza on 11/30/14

moving Los Angeles

Trying to say much obliged won't simply be sufficient, for the fantasti c clarity in your written work. I will right away snatch your rss channel to stay educated of any upgrades. moving Los Angeles

by ahmed raza on 11/30/14

rozi

Very informative post ! There is a lot of information here that can help any business get started with a successful social networking campaign !Here

by Unregistered Visitor on 11/30/14

galaxy s5 features

You have done a great job. I will definitely dig it and personally recommend to my friends. I am confident they will be benefited from this site. galaxy s5 features

by ahmed raza on 12/01/14

jack

This is such a great information for me, because i do an online job thats why i need fast Internet connection, i search how wisdom teeth removal lauderhill to bost internet speed then ireach at a right place

by Unregistered Visitor on 12/01/14

rebelmouse

Thanks for creating the page! Im positive that it will be very popular. It has good and valuable content which is very rare these days. acne no more negative review

by ahmed raza on 12/02/14

inside

The specific present exclusive on the inside of construction which will help within our companion together with we have been getting substantially effective lucrative allowing created for this matter wonderful website page. Top Custom Essay Writing

by Unregistered Visitor on 12/03/14

amazing post

The ultrasonic wind sensor is designed without mechanical parts as known with traditional "cups and vane". The digital or analog output delivers instantaneous, average, min or max value with flexible measuring rate.measuring instruments

by Unregistered Visitor on 12/03/14

golf swing reader

Awesome material! Thanks a lot, dude, for giving this so good material to us. I've just recently completed one of my projects due to the information from this post. Thanks man, once again! golf swing reader

by ahmed raza on 12/03/14

Yet Another KVO-improvement project

Actually college application must be filled by the students must be regards the content term paper writing companies on http://research-paper-writing-online.blogspot.com/ of the showing the the promises to the deliver the selection of writing the accommodated your company the compile all the information.

by Unregistered Visitor on 12/04/14

rozi

This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here! Keep up the good work. I have been meaning to write something like this on my website and you have given me an idea.γυναικεια παπουτσια αμπιγιε

by Unregistered Visitor on 12/04/14

allanmurray20

5 stars for you website. I found your site while googling. Definitely, I will recommend this to avid readers such as from outsourcing services philippines. Thanks for sharing.

by Unregistered Visitor on 12/05/14

rebelmouse

I would also motivate just about every person to save this web page for any favorite assistance to assist posted the appearance. buy marni kinrys's crack the girl code

by ahmed raza on 12/07/14

amazing post

Mie foto sono presenti in collezioni pubbliche e private come fotografo sono un autore riconosciuto. Scegliete per il giorno del vostro matrimonio un autore di fotografia contemporanea. fotografo milano

by Unregistered Visitor on 12/08/14

reviewtopoption

Really great post. I simply unearthed your site and needed to say that I have truly appreciated perusing your blog entries. Any way I'll be subscribing to your food and I trust you post again soon. http://www.reviewtopoption.com/text.php?lang=eng&id=6‬

by ahmed raza on 12/08/14

balding

You may post on the professional credentials for the blog owner. You could express it's outstanding. Your blog experience can springboard your click through. balding

by Unregistered Visitor on 12/13/14

defenders

You may post on the professional credentials for the blog owner. You could express it's outstanding. Your blog experience can springboard your click through. case management software for public defenders

by Unregistered Visitor on 12/13/14

highend

You may post on the professional credentials for the blog owner. You could express it's outstanding. Your blog experience can springboard your click through. highend

by Unregistered Visitor on 12/13/14

assets

Have to know how you are able to enhance publicity for the web web page, within an optimistic along with steady indicates? You might not understand which yet many web site workers make use of the identical key to construct visitors with regard to essentially absolutely nothing. regal assets gold

by Unregistered Visitor on 12/13/14

coaching

Awesome material! Thanks a lot, dude, for giving this so good material to us. I've just recently completed one of my projects due to the information from this post. Thanks man, once again! group coaching

by Unregistered Visitor on 12/13/14

likes

Awesome material! Thanks a lot, dude, for giving this so good material to us. I've just recently completed one of my projects due to the information from this post. Thanks man, once again! buy real instagram followers and likes

by Unregistered Visitor on 12/13/14

expert

Great post! I am actually getting ready to across this information, is very helpful my friend. Philadelphia PA seo expert

by Unregistered Visitor on 12/13/14

click

I would also motivate just about every person to save this web page for any favorite assistance to assist posted the appearance. click here now

by Unregistered Visitor on 12/13/14

juice

You may post on the professional credentials for the blog owner. You could express it's outstanding. Your blog experience can springboard your click through. breville juice fountain

by Unregistered Visitor on 12/13/14

re

Superb blog you have here but I was wanting to know if you knew of any discussion boards that cover the same topics talked about here? I’d really like to be a part of group where I can get feedback from other experienced people that share the same interest. If you have any suggestions, please let me know. Many thanks! creditos rapidos sin nomina

by Unregistered Visitor on 12/13/14

franklin7

I really located you by mistake, while I was watching on google for something else, Phone Psychic

by Unregistered Visitor on 12/14/14

franklin7

Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work. yeastrol review

by Unregistered Visitor on 12/14/14

7 Day Millionaire

The first several months of my site there were no comments; just give it time; now they come in like crazy every day! Thanks. 7 Day Millionaire

by ahmed raza on 12/14/14

rozi

Gangaur Realtech is a professionally managed organisation specializing in real estate services where integrated services are provided by professionals to its clients seeking increased value by owning, occupying or investing in real estate. I am impressed by the information that you have on this blog. It shows how well you understand this subject.http://www.assistance-bancaire.com

by Unregistered Visitor on 12/15/14

Hi

OPY World School is promoted and managed by Rao Mohar Singh Education Society with the mission to provide quality education at school level. top school in rewari

by Unregistered Visitor on 12/16/14

Hello

Colors and pigments – organic and inorganic pigments.( Beta blue, alpha blue, Rubine, Red 57, red 24, yellow1, yellow 12, yellow 10G(3), Red 3, Red 4, Red 8, Red 12, Red 53, Red 48.2, Bronze scarlet Red 53:1, Red 63, Benzidine orange 13….pigment violet 23nd 19, chrome pigment- scarlet, middle, lemon, primrose.
Rubber processing oil traders
Chemical traders in delhi
Carbon trading company delhi

by Unregistered Visitor on 12/16/14

rozi

A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.website design

by Unregistered Visitor on 12/16/14

WERKBLADE TERAZZO

Useful information shared..Iam very happy to read this article..thanks for giving us nice info.Fantastic walk-through. I appreciate this post. werkblad terazzo

by Unregistered Visitor on 12/17/14

rozi

This post is very simple to read and appreciate without leaving any details out. Great work! You completed certain reliable points there. I did a search on the subject and found nearly all persons will agree with your blog.seo experts

by Unregistered Visitor on 12/17/14

rozi

Mr. G. Scott Paterson serves as a Partner of Freepour Controls Inc. and Versa Controls. He served as the Chairman of Neulion Incorporated from January 2002 to October 2008 and Chief Executive Officer from May 2005 to October 2007.G Scott Paterson article

by Unregistered Visitor on 12/18/14

payday loans instant

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. payday loans instant

by Unregistered Visitor on 12/19/14

rozi

I have express a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon…http://www.textyourexback2.com/

by Unregistered Visitor on 12/19/14

rozi

The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.chelsea fc tickets

by Unregistered Visitor on 12/19/14

Add comment