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

franklin7

Should there be another persuasive post you can share next time, I’ll be surely waiting for it. reviews of pure garcinia cambogia

by Unregistered Visitor on 02/17/15

great

This is a very good tips especially to those new to blogosphere, brief and accurate information…www.2xmall.co.uk

by Unregistered Visitor on 02/18/15

ragahv

Consistent smooth so received numerous persons more been many correct of their thesis skills nice negotiate the real madden associated among wider balance associated accompanying stopover they might distinguish-how. buy email database

by Unregistered Visitor on 02/18/15

franklin7

Thanks for the post and great tips..even I also think that hard work is the most important aspect of getting success. wrinkle rewind

by Unregistered Visitor on 02/18/15

great

Nice Post keep continue your work really appreciating work great post and thanks for sharing informative post. self directed ira

by Unregistered Visitor on 02/18/15

fifa buy coins

been many correct of their thesis skills nice negotiate the real madden associated among wider balance associated accompanying stopover they might distinguish-how fifa buy coins

by Unregistered Visitor on 02/18/15

franklin7

Your article has proved your hard work and experience you have got in this field. top garcinia cambogia supplements

by Unregistered Visitor on 02/18/15

franklin7

I read your blog frequently and I just thought I’d say keep up the amazing work! voyance carte

by Unregistered Visitor on 02/18/15

does not require

Once the cause of diabetes became apparent, the cure seems incredibly simple. But the health care community isn't celebrating this discovery. You see, the cure does not require yet another trip to the pharmacy. The cure is at your local super market. david pearson diabete scure

by Unregistered Visitor on 02/19/15

website

http://ebookreviews.ning.com/pdf/girlfriend-activation-system-review This is a Girlfriend Activation System review and buyer's guide based on my own observations after buying the program. Continue reading below to learn more about the Girlfriend System or click here to skip the review and go directly to the official website where you can watch Christian’s video that explains how to get a girlfriend who will give herself to you completely even if you're bald, fat, ugly, socially awkward, or dead-ass broke. Christian recently revamped the program from top to bottom and created a brand new version (GFAS V2) that was just released in early 2014. The Girlfriend Activation System is one of Christian's most popular products that helps men not only find and attract amazing women, but ultimately turn them into loving, loyal, and long-lasting girlfriends. Please visit site: http://ebookreviews.ning.com/pdf/girlfriend-activation-system-review ...

by Unregistered Visitor on 02/19/15

website

how can i practice the law of attraction Manifestation Miracle: Heather Mathews Reveals Her Weirdly Effective "Lazy Person Trick" For Forcing The Universe To Give You Everything You Ask For... Written for consumers by a consumer, this fan site includes a fun and informative Manifestation Miracle review, ebook FAQ, and PDF download guide that provides an insider's look at the program and answers frequently asked questions about Heather Mathews' unique and highly popular law of attraction guide. Please click here my webpage: https://www.rebelmouse.com/manifestationmiraclereviews/ ...

by Unregistered Visitor on 02/19/15

website

https://www.rebelmouse.com/getthegirlcodereviews Get The Girl Code: Michael Fiore And Miss X Reveal A 3-Step, Scientifically Proven Evolutionary "Cheat Code" To Get Any Girl You Want Quickly, Easily, And Without Having To Lie, Manipulate, Or Play A Single Ridiculous Mind Game. Written for consumers by a consumer, this site includes a fun and informative Get The Girl Code review and buyers guide that provides an insider's look at the program and answers frequently asked questions about Michael Fiore and Marni Kinrys' unique and highly popular system for helping you bring out the very best parts of yourself to attract, date, and seduce absolutely any and every woman you want. Please visit our webpage: https://www.rebelmouse.com/getthegirlcodereviews/ ...

by Unregistered Visitor on 02/19/15

JAMS

Thanks for the post and great tips..even I also think that hard work is the most important aspect of getting success.. Homes for Sale

by Unregistered Visitor on 02/19/15

shadow fight 2 cheats

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 shadow fight 2 cheats

by Unregistered Visitor on 02/19/15

franklin7

Thanks for writing such a good article, I stumbled onto your blog and read a few post. I like your style of writing... reviews on colon cleanse

by Unregistered Visitor on 02/19/15

http://www.buysellautoapp.com

This is really a nice and informative, containing all information and also has a great impact on the new technology. Thanks for sharing it http://www.buysellautoapp.com

by Unregistered Visitor on 02/20/15

http://www.bellvilleauto.com

Good post but I was wondering if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit further. Appreciate it! http://www.bellvilleauto.com

by Unregistered Visitor on 02/20/15

shadow fight 2 hack

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 shadow fight 2 hack

by Unregistered Visitor on 02/20/15

franklin7

This is cool post and i enjoy to read this post. your blog is fantastic and you have good staff in your blog. nice sharing keep it up. garcinia cambogia pills reviews

by Unregistered Visitor on 02/21/15

franklin7

Nice information, valuable and excellent design, as share good stuff with good ideas and concepts dr oz white kidney bean extract

by Unregistered Visitor on 02/21/15

franklin7

Wow, nice post,you can find different person searching about that now they'll find enough resources by your post.Thank you for sharing to us reviews of forskolin

by Unregistered Visitor on 02/21/15

franklin7

This is the type of information I’ve long been trying to find. Oss Certification

by Unregistered Visitor on 02/21/15

franklin7

I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share. https://rebelmouse.com/girlfriendsystemreview/

by Unregistered Visitor on 02/21/15

franklin7

You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. https://www.rebelmouse.com/thediabetesprotocolreviews/

by Unregistered Visitor on 02/21/15

franklin7

These are some great tools that i definitely use for SEO work. Text Psychic

by Unregistered Visitor on 02/22/15

enterprise cloud software

You want to look at this information for the company overall as well as the individuals you sell to.enterprise cloud software

by Unregistered Visitor on 02/22/15

plus size formal clothing

Your blogs further more each else volume is so entertaining further serviceable It appoints me befall retreat encore. I will instantly grab your rss feed to stay informed of any updates. plus size formal clothing

by Unregistered Visitor on 02/23/15

Top Laptop Bags as of 2015

I wish more authors of this type of content would take the time you did to research and write so well. Top Laptop Bags as of 2015

by Unregistered Visitor on 02/23/15

gluten free ice pops

Its an amazing place to get information and I love reading to increase my knowledge. Thank you so much for information. gluten free ice pops

by Unregistered Visitor on 02/23/15

natural remedies for underactive thyroid

You have raised an important issue..Thanks for sharing..I would like to read more current affairs from this blog..keep posting.. natural remedies for underactive thyroid

by Unregistered Visitor on 02/23/15

vape pen for sale

Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. vape pen for sale

by Unregistered Visitor on 02/23/15

www.bedbathandbeyondinsider.com

I really bookmark it, pro broaden read. Appreciation pro sharing. I like it. www.bedbathandbeyondinsider.com

by Unregistered Visitor on 02/23/15

led concepts lighting halo lighting

Great post! I am actually getting ready to across this information, is very helpful my friend. Also great blog here with all of the valuable information you have. Keep up the good work you are doing here. led concepts lighting halo lighting

by Unregistered Visitor on 02/23/15

Yıldırım Turan

I'm impressed by the details that you have on this web site. It reveals how nicely you perceive this subject. Yıldırım Turan

by Unregistered Visitor on 02/23/15

franklin7

It’s appropriate time to make some plans for the future and it is time to be happy. https://rebelmouse.com/jennyboltonboostyourbustreview

by Unregistered Visitor on 02/23/15

franklin7

I admire the valuable information you offer in your articles. Thanks for posting it.. voyance directe

by Unregistered Visitor on 02/23/15

No Limit Downloads

Free android apps and games download, full versions, visit

by Unregistered Visitor on 02/23/15

Venema

This is an excellent post I seen thanks to share it. It is really what I wanted to see hope in future you will continue for sharing such a excellent post. Jakarta Wedding Photography

by Unregistered Visitor on 02/23/15

Jersey

Saya dari Indonesia, punya pemasaran tersendiri tentang :Jual Jaket Bola

by Unregistered Visitor on 02/23/15

sistemi

Its provide us with numerous fascination and enjoyment. The chance are really superb and style so quick. Their a real piece of content. The idea gives me lots of delight plus desire. sahibinden müzik sistemi

by Unregistered Visitor on 02/24/15

more tips

Thank you very much for writing such an interesting article on this topic. This has really made me think and I hope to read more. more tips here

by Unregistered Visitor on 02/24/15

view

Thank you very much for writing such an interesting article on this topic. This has really made me think and I hope to read more. view website

by Unregistered Visitor on 02/24/15

more info

Thank you very much for writing such an interesting article on this topic. This has really made me think and I hope to read more. get more info

by Unregistered Visitor on 02/24/15

website

I was in vital need to get some information on this topic and thanks to you, I’ve got that ! Thanks, once again ! visit this website

by Unregistered Visitor on 02/24/15

church

First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics , i will be always checking your blog thanks. power church

by Unregistered Visitor on 02/24/15

website

Its provide us with numerous fascination and enjoyment. The chance are really superb and style so quick. Their a real piece of content. The idea gives me lots of delight plus desire. official website

by Unregistered Visitor on 02/24/15

new years

Thank you very much for writing such an interesting article on this topic. This has really made me think and I hope to read more. new years eve nyc

by Unregistered Visitor on 02/24/15

linked here

I have bookmarked your blog, the articles are way better than other similar blogs.. thanks for a great blog! linked here

by Unregistered Visitor on 02/24/15

websites

First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics , i will be always checking your blog thanks. websites

by Unregistered Visitor on 02/24/15

job search

I don’t have the time at the moment to fully read your site but I have bookmarked it and also add your RSS feeds. I will be back in a day or two. thanks for a great site. UK job search

by Unregistered Visitor on 02/24/15

Get the

Very glad to see your article, I am very pleased and agree with their views. Do you have a good time. Get the facts

by Unregistered Visitor on 02/24/15

Acnezine

I don’t have the time at the moment to fully read your site but I have bookmarked it and also add your RSS feeds. I will be back in a day or two. thanks for a great site. Acnezine

by Unregistered Visitor on 02/24/15

click site

I have bookmarked your blog, the articles are way better than other similar blogs.. thanks for a great blog.. click site

by Unregistered Visitor on 02/24/15

junk removal

Thanks you for another great article. Where else could anyone get that kind of information in such a perfect way of writing. junk removal

by Unregistered Visitor on 02/24/15

asdd2

This is one of the most wonderful articles that I've got the chance to look at carefully and then read. I always have asked you of more articles like this and I hope that you will keep on giving me such articles. Thanks! Imprimante Second Hand

by Unregistered Visitor on 02/24/15

sadw3

This is sensational work, my friend. As a person who has been in this business for quite a long time, I have to say that I really enjoyed reading this article. It appeals to the viewers and to find these kinds of articles is really difficult. Thank you very much for doing this. Tractor de tuns iarba

by Unregistered Visitor on 02/24/15

franklin7

Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. https://www.rebelmouse.com/lindaallenyeastinfectionnomore

by Unregistered Visitor on 02/24/15

asdsa

Located in Boca Raton, FL, Parties N Fun offers creative family entertainment for Private Birthday Parties, Schools, Churchs, Community Events, Grand Openings, Corporate Functions, Family Day or any other type event servicing Fort Lauderdale, Boca Raton, Pembroke Pines, Broward, Miami Dade & Palm Beach County. party rentals broward

by Unregistered Visitor on 02/24/15

asdasd

The Exclusive Visas offers expert advice for Brazilian investors who wish to obtain a green card in the United States through the Investor Visa. visto eb5

by Unregistered Visitor on 02/24/15

asdsa

Our goals is to assist organizations in accomplishing the Great Commission by helping them clarify their vision, simplify their strategy, and maximize their leadership capabilities. visioneering

by Unregistered Visitor on 02/24/15

website

visiter le site web de l'utilisateur Desire System Review: Can You Really Turn A Woman On So Easily? What Is The Desire System By Dr. David Tian (Tan)? The Desire System by Dr. David (the Asian Rake) gives you high definition access to a closed-door seminar (4 videos, audio, and written transcripts) taught at the Aura Dating Academy which reveals a step-by-step system for lightning fast seduction based around the concept of "emotional contagion". Using these techniques you will develop the uncanny ability to turn a woman on and make her horny for you...on command. http://ebookreviews.ning.com/pdf/desire-system-review-dr-david-emotional-contagion-techniques

by Unregistered Visitor on 02/24/15

website

App Creator It looks like I am raking in money now, but the truth is that it was not always like that! Just 6 months ago I was completely struggling. I had no programming skills, no team and no money. The only thing I had was a great desire to create the game of my dreams. http://03dd4gqsm6tr0yebio1bnhhsxg.hop.clickbank.net/?tid=PHXDAN ...

by Unregistered Visitor on 02/24/15

asdasd

Here at Extravagant Baskets we are 100% committed to provide the best gift baskets on the market. We are a family owned and operated company with attention to details, you can be sure that ordering from us will be a great experience for you and especially for the ones receiving the Basket. gift basket

by Unregistered Visitor on 02/25/15

asdasd

Excellent post. The information is just classic. I feel really honorable reading this marvelous post. I just wish that you can continue this. I want to read more posts like this. Cheers. Potuguese Perfection Films HD Free Movie Watch Now Join !

by Unregistered Visitor on 02/25/15

aas2

Sometimes it is extremely difficult to explore good and useful information out there when doing research. Now I will send it to my colleagues as well. Thanks for helping me out. houses for rent in bucharest

by Unregistered Visitor on 02/25/15

franklin7

Wow! What an eye opener this post has been for me. Very much appreciated, bookmarked, I can’t wait for more! electronic cigarette sale

by Unregistered Visitor on 02/25/15

william

Thank you for taking the time to publish this information very useful! how to get more followers on instagram free instagram likes

by Unregistered Visitor on 02/25/15

william

Thanks for sharing nice information with us. i like your post and all you share with us is uptodate and quite informative, free instagram likes i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job. how to get more followers on instagram

by Unregistered Visitor on 02/25/15

asdasd

Amazing theory here! You truly do have an energy for composing, mate and you demonstrate it to us. It's a joy to peruse your articles. I trust you will keep it going on. pool service pompano beach

by Unregistered Visitor on 02/25/15

aas

Hrmm that was weird, my comment got eaten. Anyway I wanted to say that it's nice to know that someone else also mentioned this as I had trouble finding the same info elsewhere. This was the first place that told me the answer. Thanks. Garsoniere Berceni

by Unregistered Visitor on 02/26/15

franklin7

This is cool post and i enjoy to read this post. your blog is fantastic and you have good staff in your blog. nice sharing keep it up. 12th Class Result

by Unregistered Visitor on 02/27/15

franklin7

Amazing insight you have on this, it's nice to find a website that details so much information about different artists. sensor supplier

by Unregistered Visitor on 02/28/15

varied

This can be really the pushed aside website page of varied minstrels. Disorganization is actually quite visualize-ended central the penmanship. Brunette escorts 

by Unregistered Visitor on 02/28/15

asda

A lot of companies have chosen to downsize, and maybe that was the right thing for them. We chose a different path. Our belief was that if we kept putting great products in front of customers, they would continue to open their wallets. Lentile de contact colorate

by Unregistered Visitor on 02/28/15

JAMS

This is exactly what I was looking for. Thanks for sharing this great article! That is very interesting Smile I love reading and I am always searching for informative information like this! math.fullerton.edu/?film-streaming

by Unregistered Visitor on 02/28/15

asdasd

We are a technology consulting company that provides and implements complete support and solutions for small businesses throughout the Jacksonville and St Augustine areas. managed it services jacksonville

by Unregistered Visitor on 02/28/15

asad

Loving the info on this web site , you have done great job on the blog posts. Creare site Bucuresti

by Unregistered Visitor on 03/01/15

asad

whoah this weblog is terrific i really like reading your posts. carry on the terrific function! You already recognize, many persons ar seeking spherical for this data, youll be able to aid them greatly. Mobila Bucatarie

by Unregistered Visitor on 03/01/15

asad

I think it was brave (especially for a woman!!!) to go pantsless on the subway in Turkey. This was good, clean fun! (I don’t get why people get so riled up over wanting to make people smile.) knit kippot

by Unregistered Visitor on 03/01/15

asdasd

Absolutely awesome work from you here! I am extremely impressed and pleased with your work here. You've really shwon your true quality today and it is one to cherish for long! Gay Movement Liberty Films !

by Unregistered Visitor on 03/01/15

IM conference vegas

Christian church and the needs of the extremely unjust feudal society.IM conference vegas

by Unregistered Visitor on 03/01/15

sad2

Online High School Diploma Says: You got an extremely helpful website I actually have been here reading for regarding an hour. I’m an initiate and your success is incredibly a lot of a concept on behalf of me. Calculatoare Second Hand

by Unregistered Visitor on 03/02/15

franklin7

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. Virgo Horoscope

by Unregistered Visitor on 03/02/15

franklin7

Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. how to cure tinnitus naturally

by Unregistered Visitor on 03/03/15

asdasd

What a remarkable article this is. I will forever cherish reading this. This has made a special place in my heart. A world class article this. Thanks a lot for this! what is cellulite

by Unregistered Visitor on 03/03/15

franklin7

Among the lots of comments on your articles. Thanks for sharing. program

by Unregistered Visitor on 03/03/15

asad

When i appreciated people web site. It’s most to help easy to research inside, the info are going to be advantageous, and also you’re an informed content article author contrary to the majority web sites When i find out any time reckoning in this particular issue. Jucarii Bebelusi

by Unregistered Visitor on 03/03/15

franklin7

Awesome and interesting article. Great things you've always shared with us. Thanks. Torrance CPA

by Unregistered Visitor on 03/04/15

waqar

Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles Likri.com

by Unregistered Visitor on 03/04/15

deductiblecarinsurance

I got this blog site through my friends and when I searched this really there were informative articles at the place. deductiblecarinsurance

by Unregistered Visitor on 03/05/15

sd2w

Very great article you shared. thx you so much for your posting. spoturi led

by Unregistered Visitor on 03/05/15

Like it genuinely

I have bookmarked your website because this site contains valuable information in it. I am really happy with articles quality and presentation. Thanks a lot for keeping great stuff. I am very much thankful for this site. http://americaphonelookup.com

by Unregistered Visitor on 03/06/15

Add comment