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

bosphorus cruise

The countries from the long-gone Communist bloc are silently sliding to their old bad habits of living by old superstitions.bosphorus cruise

by Unregistered Visitor on 01/06/15

franklin7

I read your blog frequently and I just thought I’d say keep up the amazing work! attract him and keep him interested in you long term

by Unregistered Visitor on 01/06/15

rozi

Easily, the article is actually the best topic on this registry related issue. I fit in with your conclusions and will eagerly look forward to your next updates. Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates.קליפ בת מצווה

by Unregistered Visitor on 01/07/15

bubblegum casting

They could finally start the voyage from the feudal and tribal societies into what they are now.bubblegum casting

by Unregistered Visitor on 01/08/15

rozi

Active Business Services is an innovative energy management firm transforming the commercial and industrial energy sector.Active Energy

by Unregistered Visitor on 01/08/15

term paper

Thanks for taking this opportunity to discuss this, I appreciate with this and if you have some more information please share it with me. term paper

by Unregistered Visitor on 01/08/15

girlsdoporn

I fit in with your conclusions and will eagerly look forward to your next updates. Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates girlsdoporn

by Unregistered Visitor on 01/08/15

aq3rfedc

Great article to read this one. This is not time pass. Actually this is worth the time spent on. People also enjoy while reading this. Keep it up! eat stop eat book

by Unregistered Visitor on 01/09/15

rozi

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.παπουτσια αντρικα

by Unregistered Visitor on 01/09/15

asasc

This is some extra ordinary work from you, mate. You've shown your truly wonderful writing skills once again today. I really like this write up! I just hope I get more of these.. customized fat loss free

by Unregistered Visitor on 01/09/15

casting

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. bubblegum casting

by Unregistered Visitor on 01/09/15

tablet

I love the blog. Great post. It is very true, people must learn how to learn before they can learn. lol i know it sounds funny but its very true. . . best tablet under $100

by Unregistered Visitor on 01/10/15

payroll

This short article is perfect! If just one had been to purchase an argument on the authentication of this post, I believe that you would have to throw of your energy because this article is ideal in addition to every individual understands this kind of. church payroll processing

by Unregistered Visitor on 01/10/15

gold

Great article to read this one. This is not time pass. Actually this is worth the time spent on. People also enjoy while reading this. Keep it up! the best gold IRA company

by Unregistered Visitor on 01/10/15

Services

Great article to read this one. This is not time pass. Actually this is worth the time spent on. People also enjoy while reading this. Keep it up! SEO Services NYC

by Unregistered Visitor on 01/10/15

claiming

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. claiming benefits

by Unregistered Visitor on 01/10/15

para adelgazar

Your website is really cool and this is a great inspiring article. Thank you so much. pastillas para adelgazar

by Unregistered Visitor on 01/10/15

cigarettes

Great article to read this one. This is not time pass. Actually this is worth the time spent on. People also enjoy while reading this. Keep it up.. vapor cigarettes

by Unregistered Visitor on 01/10/15

candidvideos

I finally found great post here.I will get back here. I just added your blog to my bookmark sites. thanks.Quality posts is the crucial to invite the visitors to visit the web page, www.candidvideos.net

by Unregistered Visitor on 01/10/15

services

Great article to read this one. This is not time pass. Actually this is worth the time spent on. People also enjoy while reading this. Keep it up! Philadelphia seo services

by Unregistered Visitor on 01/10/15

precious

I must say that a succesful family owned business in this day and age is a very refreshing sight ! As well as this is a very refreshing site. precious metal ira

by Unregistered Visitor on 01/10/15

cure alopecia

Hello, i am glad to read the whole content of this blog and am very excited and happy to say that the webmaster has done a very good job here to put all the information content and information at one place. cure alopecia

by Unregistered Visitor on 01/10/15

jackson

What a blog post!! Very informative and also easy to understand. Looking for more such comments!! Do you have a facebook? I recommended it on digg. The only thing that it’s missing is a bit of new design. google plus

by Unregistered Visitor on 01/10/15

hero

Lovely deal this. I totally love the way you present your articles. You are such a gem! I want you to keep publishing more great articles like this. I will be pleased, sir. b2b digital marketing agency

by Unregistered Visitor on 01/10/15

I really

hello!! Very interesting discussion glad that I came across such informative post. Keep up the good work friend. Glad to be part of your net community. https://rebelmouse.com/mikewaldenacnenomorereview

by Unregistered Visitor on 01/11/15

girlsdoporn

he 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). girlsdoporn

by Unregistered Visitor on 01/11/15

bubblegum casting

Similar items. Marketing and branding play a major role in the creation of the luxury brands.bubblegum casting

by Unregistered Visitor on 01/13/15

william

All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks Clash of clans hack tool

by Unregistered Visitor on 01/13/15

Nice Information

Absolutely fantastic posting! Lots of useful information and inspiration, both of which we all need!Relay appreciate your work. do my assignment uk

by Unregistered Visitor on 01/13/15

bubblegum casting

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 bubblegum casting

by Unregistered Visitor on 01/13/15

franklin7

Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. funny photos wall

by Unregistered Visitor on 01/14/15

nice sharing

thank you this information helped me in learning and increase my knowledge of science that I am studying Kerajinan Tembaga

by Unregistered Visitor on 01/15/15

rozi

The worst part of it was that the software only worked intermittantly and the data wasn’t accurate. You obviously can’t confront anyone about what you have discovered if the information isn’t right.Fco Sanchez

by Unregistered Visitor on 01/15/15

great

I am quite pleased with this site. There were some concepts by the author which were essential.Eye Serum

by Unregistered Visitor on 01/15/15

franklin7

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. chunky heel cleated sole platform cut outs sandals boots

by Unregistered Visitor on 01/19/15

franklin7

I’m definitely going to look into it. Really very useful tips are provided here. Thank you so much. Keep up the good works. Psychic Blogs

by Unregistered Visitor on 01/19/15

franklin7

It is extremely helpful and interesting and very much looking forward to reading more of your work.. "Divine Frequencies from the highest Divine Realms"

by Unregistered Visitor on 01/20/15

Hi

OPJS University has been established in the year 2013, by an Act (Act No. 19 of 2008) Act No.-16 of 2013, of the Rajasthan State Legislature and is approved under section 2(f) of UGC Act 1956 vested with the authority to award Degrees, Diplomas and Certificates.
Best university in rajasthan
MPhil admission in rajasthan
Phd Courses in rajasthan

by Unregistered Visitor on 01/20/15

great

Thanks for such a great post and the review, I am totally impressed! Keep stuff like this coming. private home tuition in London

by Unregistered Visitor on 01/20/15

Thank For Post

Anda mencari produk Toko Mebel Online dengan desain minimalis ataupun klasik asli mebel jepara dengan harga murah. kami menyediakan berbagai tipe atau model produk Toko Mebel Jepara terutama dengan menggunakan material kayu jati. Disni saya menjual produk produk mebel mulai dari kursi tamu minimalis meja makan jati tempat tidur dan sebagainya. Untuk indormasi lebih lanjut anda bisa langsung menuju katalog website kami. Produk produk yang kami tawarkan mempunyai kualitas tinggi dan kami menawarkannya dengan harga yang cukup terjangkau, tentunya kualitas produk tersebut akan kami jaga. Produk Toko Mebel Minimalis selalu banyak diminati oleh banyak konsumen baik itu dari dalam maupundari luar negri. Banyak sekali turis turis asing berbondong bondong ke jepara dengan tujuan yang sama seperti anda yaitu untuk mencari produk mebelkursi.com. Kami Toko Mebel Jati sebagai produksen furniture memberikan anda kemudahan untuk berbelanja produk furniture jepara dengan aman melalui transaksi online, kami menyediakan berbagai model di dalam katalog website kami. Anda cukup duduk santai di ruamh, kami kerjakan produk pesanan anda dan tentunya kami akan memberikan garansi jika produk tidak sampai ke tangan anda ataupun produk Toko Mebel Jepara yang anda pesan tidak sesuai dengan pesanan. Setiap produk kami, kami proses dengan sangat terkontrol, baik itu dalam hal pemilihan kayu, proses kontruksi, pahatan ukir hingga proses finishing. Oleh karena itu kami sebagai perusahaan Toko Mebel Jati selalu mengutamakan kualitas produk serta meningkatkan pelayanan yang ramah kepada anda. Jepara memang dikenal sudah sejak lama sebagai kota industri Toko Furniture Online terbesar di asia hingga dunia. Kualitas produk dari jepara sudah banyak di akui oleh beberapa negara. Kota Jepara sendiri selalu menjadi langganan aktif dalam setiap pengadaan pameran furniture Toko Furniture Jepara. Banyak sekali produk produk yang di tawarkan disana, baik itu dengan model minimalis ataupun klasik. Salah satunya yang menjadi produk unggulan mebel jepara yaitu Kursi Tamu Jati Minimalis yang sangat cantik dengan keaslian ukiran khas mebel jepara yang terukir rapih dan halus di setip sudutnya. Ada juga produk unggulan lain seperti tempat tidur, meja makan, set meja makan dan banyak lainnya. Mebel Jepara Murah Oleh karena itu mulai dari lima tahun terakhir kami sudah mengembangkan untuk pembelian produk furniture dengan menawarkannya di Toko Mebel Online yang sudah berkembang hingga sekarang. Anda dapat memilih produk produk furniture tersebut diatas tadi di katalog produk kami tersebut disini produk produk kami berkualitas karena dikerjakan oleh tenaga mebel yang sudah berpengalaman dibidangnya. Untuk katalog produk anda bisa klik link berikut Mebel Jati Jepara banyak sekali yang dapat anda pilih. Untuk bertransaksi dalam pembelian Toko Mebel Jati secara online dengan kami, anda tidak perlu ragu, karena perusahaan kami sudah diakui dan mempunyai ijin kerja yang bersangkutan dengan hukum. Jadi apabila anda ada keluhan saat bertransaksi Mebel Jepara Online produk produk kami berkualitas karena dikerjakan oleh tenaga mebel yang sudah berpengalaman dibidangnya. Untuk katalog produk anda bisa klik link berikut dengan kami, jaminan keamanan 100% di toko kami Toko Mebel Jepara produk produk kami berkualitas karena dikerjakan oleh tenaga mebel yang sudah berpengalaman dibidangnya. Untuk katalog produk anda bisa klik link berikut Gambar Tempat Tidur Jati Minimalis kami sudah mulai membuka pemesanan dengan anda cukup dengan santai diruamh, anda tinggal menyodorkan bagaimana produk mebel yang anda inginkan, anda dp maka kami akan kerjakan, untuk lama proses tergantung tingkat kerumitan produk pesanan, itu smua bisasa kami tawarkan secara Tips Sebelum Membeli Furniture Minimalis Diantaranya anda dapat memesan produk produk produk kami berkualitas karena dikerjakan oleh tenaga mebel yang sudah berpengalaman dibidangnya. Untuk katalog produk anda bisa klik link berikut Meja Makan Jati Jepara dengan harga yang sesuai dengan kantong anda. produk produk kami berkualitas karena dikerjakan oleh tenaga mebel yang sudah berpengalaman dibidangnya. Untuk katalog produk anda bisa klik link berikut Toko Mebel Online memang sedang maraknya disaat perkembangan yang semakin canggih, tetapi anda jangan mudah tertipu dengan tergiur harga yang jauh dibawah harga normal. Produk unggulan mebel jepara seperti Pintu Gebyok Jati produk produk kami berkualitas karena dikerjakan oleh tenaga mebel yang sudah berpengalaman dibidangnya. Untuk katalog produk anda bisa klik link berikut rata rata dijual dengan harga standart yaitu di kisaran Rp. 7.000.000, dan untuk produk Tempat Tidur Jati Minimalis ada di kisaran harga berkisar antara Rp. 3.000.000 hingga Rp. 30.000.000 tergantung tingkat kesulitan dan bahan material seperti yang saya terangkan diatas. Tersedia model kamar set dengan type Kamar Set Minimalis Murah yang anda bisa lihat disini. Kami menyediakan layanan online untuk pemesanan Mebel Jepara Online dengan sistem online. produk produk kami berkualitas karena dikerjakan oleh tenaga mebel yang sudah berpengalaman dibidangnya. Untuk katalog produk anda bisa klik link berikut Meja Makan Jati Jepara juga tersedia di toko kami. Banyak pilihan produk produk mebel yang ada di toko kami, diantaranya Kursi Tamu Jati Jepara dengan harga yang bervariasi dan tentunya sangat kompetitif. Ada juga produk unggulan lain seperti tempat tidur, meja makan, set meja makan dan banyak lainnya. Oleh karena itu mulai dari lima tahun terakhir kami sudah mengembangkan untuk pembelian produk furniture dengan menawarkannya di Mebel Jepara Ada juga produk unggulan lain seperti tempat tidur, meja makan, set meja makan dan banyak lainnya. Oleh karena itu mulai dari lima tahun terakhir kami sudah mengembangkan untuk pembelian produk furniture dengan menawarkannya di Furniture Minimalis yang sangat cantik dengan keaslian ukiran khas mebel jepara yang terukir rapih dan halus di setip sudutnya. Ada juga produk unggulan lain seperti tempat tidur, meja makan, set meja makan dan banyak lainnya. Oleh karena itu mulai dari lima tahun terakhir kami sudah mengembangkan untuk pembelian produk furniture dengan menawarkannya di Toko Mebel Jepara

by Unregistered Visitor on 01/21/15

articl

The item includes an incredible content publisher along with exceptional pure intuition to talk about articles or blog posts along these lines. Love regarding discussing this specific. Office Cleaning Abu Dhabi

by Unregistered Visitor on 01/21/15

ewtwetwe

Here at Toronto Secure Locks we offer an unbeatable fast Professional Locksmith service, so if you’re locked out, need your locks changing or replacing or even need a safe opening we can help any time of day, we also provide an Access control, CCTV http://localcalgarylocksmith.com/ http://0544342878.com http://mysaite.ru

by Unregistered Visitor on 01/21/15

Rachel

UEFA Dinar Cup 2012 may be a famous world football tournament hosted by just Poland together with Ukraine, this tournament has been said to launch 8th June 2012 and definitely will last before 1st June 2012. In the tournament we will see 16 good nations competing to your Euro Drink 2012. www.scorespro.com

by Unregistered Visitor on 01/23/15

franklin7

This is a truly good site post. Not too many people would actually, the way you just did. Forex

by Unregistered Visitor on 01/24/15

rozi

I’m excited to uncover this page. I need to to thank you for ones time for this particularly fantastic read !! I definitely really liked every part of it and i also have you saved to fav to look at new information in your site.http://www.kuchniaplus.pl/program/program-tv/full/be-active-ewa-chodakowska_43048

by Unregistered Visitor on 01/24/15

rozi

Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing. milagro para el acne descargar

by Unregistered Visitor on 01/24/15

william

Thanks for this article very helpful. thanks. latest News

by Unregistered Visitor on 01/25/15

amazing post

Onze luxe maison d hotes ligt op twee hectaren tuin met een binnenplaats vol bloemen, in een rustige bosrijke omgeving met petanque baan . B&B Catus Cahors Frankrijk

by Unregistered Visitor on 01/26/15

LED Concepts Lighting

Yes i am totally agreed with article kitchen suppliers essex and i want say that this article is very nice and very informative article.I will make sure to be reading your blog more.LED Concepts Lighting

by Unregistered Visitor on 01/27/15

Get Rid of Double Chin

Yes i am totally agreed with article and i want say that this article is very nice and very informative article.Get Rid of Double Chin

by Unregistered Visitor on 01/27/15

find more info

I definitely savored every part of it and i also have you book marked to look at new stuff in your website. find more info

by Unregistered Visitor on 01/27/15

a fantastic read

I was exactly searching for. Thanks for such post and please keep it up. Great work.a fantastic read

by Unregistered Visitor on 01/27/15

brown mackie

Great, This specific net webpage is seriously thrilling and enjoyment to learn. I’m an enormous fan from the subjects mentioned. brown mackie

by Unregistered Visitor on 01/27/15

goose down comforter guide

Thanks for the blog loaded with so many Stopping by your blog helped me to get what I was looking for. goose down comforter guide

by Unregistered Visitor on 01/27/15

Jogar Pou

So luck to come across your excellent blog. Your blog brings me a great deal of fun.. Good luck with the site. Jogar Pou

by Unregistered Visitor on 01/27/15

franklin7

I found this is an informative and interesting post so i think so it is very useful and knowledgeable. www.seopoli.com

by Unregistered Visitor on 01/27/15

saad2

wow, awesome post, I was wondering if there is a way to enlarge my penis quickly. and found your site by google, learned a lot, now i’m a bit clear. bookmarked and also signed up your rss. keep us updated. masina de tuns iarba Husqvarna

by Unregistered Visitor on 01/27/15

More about the author

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

by Unregistered Visitor on 01/28/15

ree

security guard provider in mumbai | famous vastu consultant in mumbai | housekeeping contractors thane | swimming pool cleaning services | residential interior designers | corporate gifts india mumbai | autoriders rent car mumbai | industrial safety products dealers in mumbai | lonavala couple tour | fire fighting equipments dealers in pune | plastic pouches manufacturers mumbai |

by Unregistered Visitor on 01/29/15

william

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. cochack.biz

by Unregistered Visitor on 01/29/15

william

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. Clash of clans hack

by Unregistered Visitor on 01/31/15

franklin7

I have read a few of the articles on your website now, and I really like your style of blogging. Ottawa Movers

by Unregistered Visitor on 01/31/15

Add comment