TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Simple iOS Key Value Observing (KVO) Explained

11 点作者 gherlein超过 13 年前

4 条评论

LinaLauneBaer超过 13 年前
This explanation is good to start with but the reader has to read a much more in depth explanation afterwards because the example given by the author is flawed:<p>He is registering an observer without using a context. If you remove the observer without specifying a context then bad things can happen if two observers do the same. For this reason there is a new method for this in 10.7 and this fact should have been mentioned because so many people have wasted so many hours debugging KVO.
评论 #3424451 未加载
liquidchaz超过 13 年前
Another tip I would add is that messages sent to observeValueForKeyPath:ofObject:change:context: may not occur on the main thread.<p>For example, if you have multiple operations in an NSOperationQueue (like web service requests), and one of those operations updates a property on the object being observed, the observing method is called on that operation's thread. This can cause issues if the observing method needs to update the GUI. In this case, you'd need to use GCD to make sure the contents of the listening method execute on main thread (or alternatively, make sure the object's property is updated on the main thread).
chriseidhof超过 13 年前
It's a very minimal explanation. But it's good that more people know about KVO, it can really simplify your code. It becomes even nicer when you use something like BlocksKit, because then you don't have to do the annoying checks in the observer method: <a href="http://dizzytechnology.com/data/BlocksKit/Categories/NSObject+BlockObservation.html" rel="nofollow">http://dizzytechnology.com/data/BlocksKit/Categories/NSObjec...</a>
gherlein超过 13 年前
Thanks for the elaborations. It sounds like 10.7/iOS5 solves some of the issues brought up at &#60;a href="<a href="http://www.mikeash.com/pyblog/key-value-observing-done-right.html&#62;here&#60;/a&#62;" rel="nofollow">http://www.mikeash.com/pyblog/key-value-observing-done-right...</a>.